3

In MDN in this example on parseInt method

console.log(parseInt(4.7 * 1e22, 10)); // Very large number becomes 4
console.log(parseInt(4.7 * 1e20, 10)); //result is 470000000000000000000

or small number than 20 it give me expected result what is reason for this ?

9
  • 2
    4.7 * 1e22 is already an integer. Why are you parsing that as an integer again? parseInt converts the first argument into a string (because it expects one) and parses that as an integer again. The actual result of 4.7 * 1e22 is 4.7e+22. Commented May 16, 2017 at 14:38
  • What is expected result? Commented May 16, 2017 at 14:44
  • ok i got it but also there is conflicting in converting 4.7e+22 why this give 4 and 4.7e+2 give 470 Commented May 16, 2017 at 14:45
  • The answer to the question, "why can some values be accurately represented and other, larger numbers, not is because of how the variable storage is allocated in memory. There is a maximum value that can be stored for a Number type in javascript as discussed here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. The reason this is a comment and not an answer is because the number the second line puts out is, in fact, larger than that and I am not sure why. I am interested to hear the answer as well. Commented May 16, 2017 at 14:47
  • expected result for me is to 47000000000000000000000 not just 4 Commented May 16, 2017 at 14:47

2 Answers 2

3

With help from @Xufox

console.log(parseInt(4.7 * 1e22, 10)); // Very large number becomes 4
console.log(parseInt(4.7 * 1e20, 10)); //result is 470000000000000000000

What's happening here in steps:

  • Calculation is done (4.7 * 1e20) and (4.7 * 1e22)
  • Result of calculation is stringified by the JavaScript engine so it can be passed to parseInt
  • The string is parsed back to a number
  • Finally it's logged

JavaScript truncates every number with more than 20 digits using the scientific notation. That means the result of the calculations are:

  • 470000000000000000000
  • 4.7e22

These are stringified before being passed to parseInt:

  • "470000000000000000000"
  • "4.7e22"

These are strings, not numbers. parseInt will now ignore everything after the dot in the second value and return 4.

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

Comments

1

It fails after 20 digit integer number with radix 10.

You may have a look to the description of parseInt:

Because some numbers include the e character in their string representation (e.g. 6.022e23), using parseInt to truncate numeric values will produce unexpected results when used on very large or very small numbers. parseInt should not be used as a substitute for Math.floor().

From the standard ECMA 252 V 5.1 15.1.2.2 parseInt (string , radix)

Step 13:

Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the mathematical integer value that is represented by Z in radix-R notation.)

...

NOTE

parseInt may interpret only a leading portion of string as an integer value; it ignores any characters that cannot be interpreted as part of the notation of an integer, and no indication is given that any such characters were ignored.

var x = 5.7 * 1e20;
console.log(x);
console.log(parseInt(x, 10));

x = 5.7 * 1e21;
console.log(x);
console.log(parseInt(x, 10));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.