If you want a number, there is no way to drop 00000000000001
to get 0.37
exactly. Like for other languages (most hardware supports IEEE 754), the number 0.37
does not exists. If scanning 0.37
it is rounded to the nearest possible number: 0.37.toPrecision(18)
shows "0.369999999999999996"
.
Because of this, toFixed()
returns a string and not a number. So it is nonsense to use toFixed()
and convert the result back to a float.
The easiest solution is to live with these unexact values and use toFixed()
for output only (that is the idea of toFixed()
).
Another solution for e.g. money is, to store all values as cent/penny and divide the values by 100 only on output: ($cent/100).toFixed(2)