According to JS/ECMAScript
specification, the Number type
uses double-precision floating point which has 64-bit
format (binary64
), consists of a sign bit (determines positive or negative value), 11
exponent bits and 52
fraction bits (each digit represents 4
-bits, hence 64
-bit has 16
digits):
The Number type representing the double-precision 64-bit format IEEE
754-2008 values as specified in the IEEE Standard for Binary
Floating-Point Arithmetic.
The maximum positive number which can be represented properly using double precision is 9007199254740992
, which can be achieved by using Math.pow(2, 53)
. If the number range is between Math.pow(2, 53)
and Math.pow(2, 54)
(or between Math.pow(2, -53)
and Math.pow(2, -54)
), only even numbers can be represented properly because the exponent bits will affect LSB
(least-significant bit) on the fraction bits.
Let's review the large number part:
var x = 12345678912345.6789
var x = new Number(12345678912345.6789)
This number contains more than 52
fractional bits (72
bits in total), hence the rounding used to keep the fractional bits to 52
.
Also with this decimal number:
var x = new Number(.12345678912367890)
This number contains 68
fractional bits, hence the last zero is chopped off to keep 64
-bit length.
Usually numeric representation larger than 9007199254740992
or smaller than 1.1102230246251565E-16
are stored as literal strings instead of Number
. If you need to compute very large numbers, there are certain external libraries available to perform arbitrary precision arithmetic.
If you want to cast more then 16 points after the decimal point you can either: