Skip to main content
1 of 1
chux
  • 33.7k
  • 2
  • 38
  • 92

Questionable goal

Code suffers from/abuses double rounding.

Javascript numbers use binary64 encoding. That provides about 1019 different values.
10.395 is not one of them as it is not a dyadic rational, an integer times a power of 2.

In the vicinity of 10.395 are encodable values:
10.394999999999999573674358543939888477325439453125
10.3950000000000013500311979441903531551361083984375

As a string "10.395" converted to a number, it is convert to the closer, yet lesser one.

With OP's code, both of these return a value near 10.40.

So if number is a string like "10.395", 10.40 is a good result. Yet if number is a number prints like 10.395, it really is 10.394999... and 10.39 is a good result.

chux
  • 33.7k
  • 2
  • 38
  • 92