Puzzle time: Yes, this is not good code, but do you know why this quirk occurs? Answer will be posted (or retweeted) later :-) Clue: An intermediate step occurs in the transformation of the supplied number.
String(0.000001) = "0.000001" => parseInt: 0 String(0.0000001) = "1e-7" => parseInt: 1
Had you used #TypeScript, it wouldn't have even compiled. parseInt isn't quirky. Implicit coercion is. parseInt doesn't even take number as the argument! It takes string!
parseInt(val) 1. Converts val to string. Numbers get converted to exponential notation if they have too many digits (like 0.0000001 has). 2. Strips anything after, and including, the first non-digit 3. Converts to integer 0.000001→"0.000001"→"0"→0 0.0000001→"1e-7"→"1"→1
"parseInt" function convert value to the string. So String(0.0000001) will return 1e-7, and parseInt('1e-7') will return 1.
parseInt(String(0.0000001))==parseInt(0.0000001) String(0.0000001)=>"1e-7" so parseInt(0.0000001) == 1 but parseInt(String(0.000001))==parseInt(0.000001) String(0.000001)=>"0.000001" so parseInt(0.000001) == 0 if we use parseInt(Num(0.0000001)) == 0
Because it first use toString method. And it can’t parse numbers less than 1e-6 and bigger than 1e21.
The number is first converted to a string — and unfortunately 0.000001 is converted to exponent format (1e-7) which parseInt will read and take the ‘1’.
Interesting! Just sent in a pull request with this quirk to #wtfjs github.com/denysdovhan/wt… Thanks @JavaScriptDaily
the second number will be converted to the exponential notation. parseInt takes a string as an arg so it is basically parseInt(‚1e-1000‘) or something like that. Everything after the e will be cut off.
not that it makes a difference in this case, but you should always practice safe `parseInt` by using a radix...
parseInt takes a string as first parameter. 0.0000001 is transformed to 1e-7... so parseInt("1e-7") and that's why the result is 1
United States 트렌드
- 1. Cheney 135K posts
- 2. Election Day 130K posts
- 3. Jakobi Meyers 4,310 posts
- 4. #csm219 3,453 posts
- 5. Mamdani 585K posts
- 6. Logan Wilson 8,378 posts
- 7. Shota 17.6K posts
- 8. Cuomo 280K posts
- 9. New Jersey 212K posts
- 10. Iraq 58.2K posts
- 11. GO VOTE 101K posts
- 12. #TheView N/A
- 13. Rickey 2,003 posts
- 14. New Yorkers 85K posts
- 15. #Election2025 3,135 posts
- 16. #tuesdayvibe 2,637 posts
- 17. Waddle 6,237 posts
- 18. New York City 174K posts
- 19. GOOD MORNING MINTO N/A
- 20. Rolex 18.4K posts
Something went wrong.
Something went wrong.