i have an array with some double value inside:
private double speed[] = {50, 80, 120, 70.3};
public void printSpeed() {
for(int i = 0; i<=speed.length-1; i++ ) {
System.out.println(speed[i]);
}
}
output
50.0
80.0
12.0
70.3
wanted output
50
80
12
70.3
How to do print the exactly string of the array?
One thing to note to start with: the final value will not be exactly 70.3, as that can’t be exactly represented in a
double. If exact decimal values are important to you, you should consider usingBigDecimalinstead.It sounds like you want a
NumberFormatwhich omits trailing insignificant digits:(As an aside, I would strongly advise you to keep the
[]for array declarations with the type information –double[] speedsinstead ofdouble speeds[]. It’s much more idiomatic Java, and it puts all the type information in one place.)