I am trying to get a desired output based on a variable input. I can get close to what I want but there seems to be an issue with rounding a number.
What I want by example (input > output).
30 > 30
30.0 > 30
30.5 > 30,5
30.5555 > 30,6
30.04 > 30
The problem is that the last one comes back as 30.0. Now I understand why this is happening (because of the rounding up/down)
My code:
private String getDistanceString(double distance) {
distance = 30.55;
DecimalFormat df = new DecimalFormat(".#");
if (distance == Math.floor(distance)) {
//If value after the decimal point is 0 change the formatting
df = new DecimalFormat("#");
}
return (df.format(distance) + " km").replace(".", ",");
}