2

I tried to parse a string to float having 15 decimal places, and with 14 decimal places. Below is the console output

parseFloat('99.000000000000001')
//99
parseFloat('99.00000000000001')
//99.00000000000001

Why does removing one decimal make this difference?

Sujit Y. Kulkarni
  • 1,186
  • 3
  • 22
  • 37

1 Answers1

1

Well there is a bit limit to what numbers Javascript can handle (until we get BigInt64 or if you use a library like decimal.js). So since it cannot handle more decimals it just truncates at a point. If you would make a bigger number you would see less decimals. If this then leads to the number being "exacly" 99 in your case javascript will correctly show it as 99 and not 99.00000000000 since we only have Number and not float, decimal, int, etc.

parseFloat(1234567.123456789);
// 1234567.123456789
parseFloat(12345678.123456789);
// 12345678.12345679
parseFloat(123456789.123456789);
// 123456789.12345679
parseFloat(1234567891.123456789);
// 1234567891.1234567
parseFloat(12345678912.123456789);
// 12345678912.123457
parseFloat(123456789123.123456789);
// 123456789123.12346

Edit: actually it seem to round and not drop.

I hope that explains things.

JGoodgive
  • 1,068
  • 10
  • 20