4

Possible Duplicate:
Large numbers erroneously rounded in Javascript

I am experiencing some strange behavior when working with numbers in javascript. When I use the following code:

<a href="javascript:console.log({'id':9200000000032337}.id);"> CLICK HERE </a>

I get the number 9200000000032336 in my console. I think it most be something with rounding or max values for numbers, but I don't understand it completely. Anyone?

2
  • 1
    what code? is clearly enough! Commented Oct 30, 2012 at 7:07
  • @sunleo, the code is only the html I have put in the question, the result is in the console if you click the <a> tag. Commented Oct 30, 2012 at 7:09

1 Answer 1

11

I'm not a Javascript expert, but it sounds like your number is being stored as an IEEE-754 64-bit floating point number. Certainly that's what I get from C# code which will display the exact value of a double:

double d = 9200000000032337;
Console.WriteLine(DoubleConverter.ToExactString(d));

(Using my own DoubleConverter class.) My output is the same as yours: 9200000000032336

Floating point values only ever hold a certain number of significant digits accurately - and when the numbers get high enough, even integers can't be stored exactly.

ECMA-262 seems to confirm this:

4.3.19
Number value
primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value

and from section 7.8.3 (numeric literals):

A numeric literal stands for a value of the Number type. This value is determined in two steps: first, a mathematical value (MV) is derived from the literal; second, this mathematical value is rounded as described below.

Section 8.5 contains more details.

Sign up to request clarification or add additional context in comments.

1 Comment

thats exactly what it is. as bigger the number, as lower is the precision.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.