Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

asp.net mvc - Why max digits with decimal in JavaScript are only 16

I came across this issue a while back when I was testing some HTML forms. The Maximum number of digits in JavaScript Number with a decimal point is only 16

I have tried the following

var x = 12345678912345.6789

x is 12345678912345.68 - 16 Digits only

var x = 123456789123.6789

x is 123456789123.6789 - 16 Digits only

new Number(12345678912345.6789)

12345678912345.68 - 16 Digits only

new Number(123456789123.6789)

123456789123.6789 - 16 Digits only

if you count the total digits, they are 16. If you increase digits before decimal point, the digits after decimal point get rounded

Similarly

new Number(.12345678912367890)

is 0.1234567891236789 - 16 Digits only (notice trailing 0 is missing)

Which makes me deduce that I can only have 16 Digits in a number with Decimal in it. If I try to add more digits, the number starts to round

I also observed that when I serialize a number with decimal into JSoN in ASP.NET MVC, it also converts the number to Max 16 Digits and rounds the rest

My Question: why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Further reading:

ECMAScript: Number Encoding

ECMAScript: Working with large integers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...