0

I have a big problem with large values that I have to change from string to number with two decimal places.

Examples:

parseFloat('99999999999999.99') 

What I get: 99999999999999.98

What I want to get: 99999999999999.99

parseFloat('99999999999999000000.00').toFixed(2)

What I get: '99999999999999000576.00'

What I want to get: '99999999999999000000.00'

parseFloat('999999999999999999999.99')

What I get: 1e+21

What I want to get: 999999999999999999999.99

Also, I have a problem with rounding:

(9.555).toFixed(2)

What I get: '9.55'

What I want to get: '9.56'

Is there any library that can help me with this problem? I really want the resulting value to be of type number.

I know is BigInt type, but I have decimals. I know is bignumber library, but the resulting values are strings.

Domiku
  • 21
  • 4
  • It looks like you're running into the limits of JS number precision – evolutionxbox Apr 26 '22 at 07:12
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – evolutionxbox Apr 26 '22 at 07:12
  • "_I really want the resulting value to be of type number._" That's not possible. The way the Number type values are stored doesn't allow for those exact numbers. That is why those libraries return strings. – Ivar Apr 26 '22 at 07:23

0 Answers0