0

I tried Math.round and String.format function to round float/double values.

1) Using Math.round function

Math.round(floatValue* 10.0) / 10.0;

For floatValue 40.55, result is 40.6 //Correct & Expected result.

For floatValue 30.05, result is 30.0 //Incorrect result. Expected result: 30.1

2) Using String.format function

String.format("%.1f", floatValue);

For floatValue 40.55 result is 40.6 //Correct & Expected result.

For floatValue 59.65 result is 59.6 //Incorrect result. Expected result: 59.7

Why same function is behaving differently for different float values.

Tejas Shelke
  • 229
  • 2
  • 9

1 Answers1

0

In can be visualized with

    float fv = 30.05f;
    System.out.println((fv));
    System.out.println((fv * 10.0));
    System.out.println(Math.round(fv * 10.0));
    System.out.println(Math.round(fv * 10.0) / 10.0);

result

30.05
300.49999237060547
300
30.0

As you can see 30.05 is actually stored as 30.49999237060547 so that is why it appears that it is rounding down. See the more concise explaination by @EricPostpischi in the comments.

See https://rules.sonarsource.com/java/tag/misra/RSPEC-1244

and

What's wrong with using == to compare floats in Java?

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Yeah, I also found that [30.05f * 10.0 = 300.49999237060547]. That's the issue. I made it to work by changing code Math.round(this * 10.0f) / 10.0f. – Tejas Shelke Feb 05 '19 at 09:39
  • 30.05 is not stored as 300.49999237060547. First, that is off by a factor of 10, Second, it is not correct to say that one number is stored as another number. If somebody assigned −3 to an `unsigned char`, so that 253 were in the `unsigned char`, we would not say that −3 is “stored as” 253. We recognize that −3 is converted to `unsigned char`, and the result is a different number, 253. Similarly, when `30.05` is read from source code, it is converted to 30.049999237060546875, and then 30.049999237060546875 is stored in `fv`. It is a different number and is **not** a representation of 30.05. – Eric Postpischil Feb 05 '19 at 15:09
  • @EricPostpischil Thank you for posting out my typo and also for explaining the problem in a lot better style than I did. – Scary Wombat Feb 05 '19 at 23:44