2

First off this is the code I use :

public static float roundAt(float value ,  int digits) {

    double pow = Math.pow(10, digits);
    double retvalue;

    retvalue = (value * pow); 
    retvalue = Math.round(retvalue);
    retvalue = retvalue / pow;

    return (float) retvalue;        
}

So using this method I do the following

if I round these values:

roundAt(0.495f,2) = 0.5
roundAt(1.495f,2) = 1.5
roundAt(2.495f,2) = 2.49

I try to understand the logic behind this but I can't. I want the rounding to act the same everytime so I would get 2.5 with roundAt(2.495f,2). Any idea what's going on ?

Thank you

Shadow
  • 3,926
  • 5
  • 20
  • 41

2 Answers2

0

The reason for that is, that you calculation...

retvalue = (value * pow); 

...leads to...

2.495 * 100.0 = 249.4999885559082

Using a double instead of a float will fix that specific problem. Neither float nor double are exact values, but float is even less exact than double and will lead to errors more easily. If you are curious, you will find much material on floating point arithmetics out there.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
0

If you do 2.495f * 100.0 you will notice that the result is 249.4999885559082. Since the decimal part is less than .5, this gets rounded down to 249.0 that, when divided by 100.0, gives 2.49.

If you want more information on how to round a double, check this other answer.

Community
  • 1
  • 1
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48