Need to round float to one decimal point. Why is it so difficult?

The reason is that floating point numbers aren’t decimal.
It’s literally impossible to represent 0.1 exactly in a floating point number. Computer can’t do it.
0.5, 0.25, 0.125, 0.0625, and other numbers that are even fractions of 2 are exactly representable.

The problem is that you’re doing this:

  1. Try to represent a value that’s exactly 0.1
  2. Present the value that’s stored above exactly

What you should be doing is:

  1. Represent whatever the number actually is
  2. Present the number with rounding in presentation, for example using ToText (float)

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

1 Like