0

I have big numbers coming from the backend service in my JS application. I don't want to lose any of the digits after the decimal place.

However, when a number like 999999999999999.99 is assigned to a variable in JS, it rounds it to 1000000000000000 and 99999999999999.99 turns to 99999999999999.98.

Both numbers are within the limits. i.e. less than Number.MAX_SAFE_INTEGER

See the above in action in the screenshot below:

enter image description here

Can anybody shed some light on it, please?

A J Qarshi
  • 2,772
  • 6
  • 37
  • 53

1 Answers1

1

MAX_SAFE_INTEGER only guarantees precision for integers below it, not decimals.

In fact there are no decimals in Javascript only floating point numbers. When you try to represent a number with too much precision it'll just be rounded to the nearest 64 bit floating point number.

If you actually want decimals you will need to use a decimal library like decimal.js or you could write your own.

Jamesernator
  • 778
  • 7
  • 13