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 ?
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:
stringified by the JavaScript engine so it can be passed to parseIntJavaScript truncates every number with more than 20 digits using the scientific notation. That means the result of the calculations are:
These are stringified before being passed to parseInt:
These are strings, not numbers. parseInt will now ignore everything after the dot in the second value and return 4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With