It’s because parseInt is expecting a string, so the decimal gets converted to a string, and
0.0000005.toString()
returns5e-7
.Good old JS, because exceptions are a sin.
What language is that so I can avoid it.
We all know what it is.
lol it’s js of course
If anyone’s wondering why:
>> 0.000005 0.000005
>> 0.0000005 5e-7
Yup. parseInt is for strings.
Math.floor, Math.ceil, Math.round or Math.trunc are for numeric type “conversions” (cause its still a float)Nah, it’s stupid either way.
“5e-7” is not an int to be parsed. Neither is “0.5”.
People give JS a lot of shit. And I do too. But it’s meant to continue running and not fail like C code would. It’s meant to basically go “yeah, sure I’ll fuck with that” and keep trucking.
So you can always make it do stupid shit when you use it a stupid way.
Is this bad? Maybe. Was it the intention of the language? Absolutely.
Typescript fixes a lot of these headaches. But I feel like JS is doing exactly what it was meant to do. Keep trucking even when the programmer asks it to do stupid shit.
If you’re using JS and don’t understand this then it’s your fault and not the languages fault.
Do we all want to live in a world of typedefs as strict as C and have our webpages crash with the slightest unexpected char input? Probably not.
We don’t notice all the time JS goes “yeah I can fuck with that” and it works perfectly. We only notice the times it does that and it results in something silly.
TLDR: JS does what it was made to do. And because of that it looks absolutely ridiculous sometimes.
Ah, folly of untyped systems. Tbh this behaviour makes sense given the rules implemented within the language. Anything passed to parseInt is casted to string and then parsed.
Is it shitty behaviour - yes. Does it make sense in given the language implementation - yes.
With this reasoning, would you say everything that a computer does makes sense, because it always follows the implementation? Or am I missing something?
Bar space-level problem where radiation can randomly shift your bits, I think yes.