Why parseInt(0.0000005) returns 5 in JavaScript
![Why parseInt(0.0000005) returns 5 in JavaScript](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2FAsset-3%404x.png&w=3840&q=75)
We can't believe our eyes when we see it:
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-8.png&w=3840&q=75)
You jump out of your skin and my mouth hangs open for over 10 minutes.
Just when we thought we knew everything about parseInt
and the idiotic things only JavaScript did?
The funny thing is it returns 0
for four and five zeroes after the decimal point.
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-9.png&w=3840&q=75)
But just add one more zero and it starts acting crazy?
The difference from Number
is even more stark.
✅ parseInt
vs Number
-- four and five zeroes:
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-2.png&w=3840&q=75)
But:
❌ parseInt
vs Number
-- six zeroes:
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-3.png&w=3840&q=75)
But we figure it out eventually.
parseInt: The missing parts
By understanding how the parseInt
function work:
It takes two arguments: the string and the base or radix.
parseInt(string)
parseInt(string, radix)
Look what it does for 6 zeroes when it's a string:
parseInt('0.5') // 0
parseInt('0.05') // 0
parseInt('0.005') // 0
parseInt('0.0005') // 0
parseInt('0.00005') // 0
parseInt('0.000005') // 0
// 6
parseInt('0.0000005') // 0
parseInt('015') // 15
parseInt('015', 8) // 13
But for Number
:
Number('0.5') // 0.5
Number('0.05') // 0.05
Number('0.005') // 0.005
Number('0.0005') // 0.0005
Number('0.00005') // 0.00005
Number('0.000005') // 0.000005
// 6 !
Number('0.0000005') // 0.0000005
Number('015') // 15
But what happens when it's a number?
A number like 0.0000005?
Step 1: Convert the number to a string
Number gets coerced to a string!
So look what parseInt
does to 0.0000005
:
String(0.5); // => '0.5'
String(0.05); // => '0.05'
String(0.005); // => '0.005'
String(0.0005); // => '0.0005'
String(0.00005); // => '0.00005'
String(0.000005); // => '0.000005'
String(0.0000005); // => '5e-7' 👈 this is important
Step 2: Do the actual rounding
So:
parseInt(0.0000005)
→ parseInt('5e-7')
And now see what happens for '5e-7'
for parseInt
:
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-4.png&w=3840&q=75)
Why?
Because of how parseInt
works: It interprets only the leading portion of the string as an integer value.
It doesn't recognize e
, so it ignores it and everything onwards.
This is same reason why:
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-6.png&w=3840&q=75)
parseInt(999999999999999999999)
→parseInt(1e+21)
→1
parseInt(0.0000007)
→parseInt(7)
→7
So Number
is your best bet if you wanna avoid surprises when converting a string to a number
![](/_next/image?url=https%3A%2F%2Fwp.codingbeautydev.com%2Fwp-content%2Fuploads%2F2024%2F07%2Fimage-7.png&w=3840&q=75)
And if you need just the integer part, Math.floor()
is here for you:
Math.floor(0.5); // 0
Math.floor(0.05); // 0
Math.floor(0.005); // 0
Math.floor(0.0005); // 0
Math.floor(0.00005); // 0
Math.floor(0.000005); // 0
Math.floor(0.0000005); // 0 ✅ perfect
See also
- 5 amazing new JavaScript features in ES15 (2024)
- How to HACK JavaScript with Well-Known Symbols (5 ways)
- Why ['1', '5, '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
- The 5 most transformative JavaScript features from ES13
- The 5 most transformative JavaScript features from ES8
- The 5 most transformative JavaScript features from ES9