Ad

How Do I Deal With Number Greater Than 2^53 In Javascript?

- 1 answer

I have looked around at questions asking about the maximum and/or minimum limit to numbers in JavaScript. They all say things about how the limits are -2^53 to 2^53 inclusive, but none of them say if there is any way to deal with numbers outside of that range if you need to, except one answer that said you can change it into a string but it wasn't very specific and I didn't understand it.

If anyone can either expound on the idea of changing it into a string of offer a new one, that would be very helpful.

Also, as a side note that it probably much simpler. How do you make sure that numbers are not displayed in scientific notation and only in standard form?

Ad

Answer

Javascript numbers are represented internally as IEEE 754 double-precision floating point numbers.

On an absolute scale, they can represent numbers from -21023 to 21023. For small numbers like 1, they have a very high precision, down to steps of 2-52. However, as the magnitude of a number increases, the precision of the representation decreases. The ±253 range you've read about is the maximum range of integer representations — once a number exceeds 253, the minimum "step" increases from 1 to 2.

If you need to exactly represent integers greater than 253, you will need to use a Javascript bignum library. However, if you just need to represent floating-point values, you're OK; the default number type will do just fine.

Ad
source: stackoverflow.com
Ad