Why Does parseInt(0.0000005) Return 5? Uncovering JavaScript’s Quirks
This article explains JavaScript’s parseInt function, demonstrates how it parses integer strings and binary numbers, reveals its unexpected behavior with small floating‑point values due to automatic string conversion and exponential notation, and recommends using Math.floor for reliable integer extraction.
parseInt() is a built‑in JavaScript function that parses an integer from a numeric string. For example:
const number = parseInt('100');
number; // 100As expected, the string '100' is parsed to the number 100.
parseInt(numericalString, radix) also accepts a second argument radix (2‑36) indicating the base of the string, defaulting to 10.
Parsing a binary string:
const number = parseInt('100', 2);
number; // 4parseInt('100', 2) interprets '100' as base‑2, returning 4 in decimal.
1. Mysterious parseInt behavior
If the first argument is not a string, JavaScript performs ToString conversion before parsing.
parseInt(0.5); // => 0
parseInt(0.05); // => 0
parseInt(0.005); // => 0
parseInt(0.0005); // => 0
parseInt(0.00005); // => 0
parseInt(0.000005); // => 0Extracting the integer part of small floats works as expected, but what about 0.0000005?
parseInt(0.0000005); // => 5parseInt() parses 0.0000005 as 5 because the number is first converted to the exponential string “5e‑7”, and parseInt reads the leading “5”.
2. Solving the parseInt mystery
Examining the conversion of various floats to strings shows that 0.0000005 becomes “5e‑7”, unlike other values.
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'Parsing the exponential string yields:
parseInt(0.0000005); // => 5
// similar to
parseInt(5e-7); // => 5
// similar to
parseInt('5e-7'); // => 5Because parseInt always converts its first argument to a string, any float smaller than 1e‑6 is represented in exponential notation, and parseInt extracts the leading integer.
For safely extracting the integer part of a float, use Math.floor():
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); // => 03. Conclusion
parseInt() converts numeric strings to integers, but when used on floating‑point numbers it can produce surprising results; therefore Math.floor() is recommended for reliable integer extraction.
Now, can you explain why parseInt(999999999999999999999) equals 1? Share your answer in the comments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
