Why SOMETIMES … (String == Number) ….
March 10th, 2008 by
Jaap Kooiker
So I encountered a really weird, but probably “explainable” problem in AS2 yesterday.
We all know that
trace("200.1" == 200.1) //true
and
trace("200.2" == 200.2) //true
trace(”200.3″ == 200.3) //true
trace(”200.81″ == 200.81) //true
trace(”200.81″ != 200.81) //false
This above works fine…but the following is what scares me the most….
trace("200.82" == 200.82) //false
Why the hell is the above false ???? And the really weird thing is
trace("200.82" != 200.82) //false
also false ???
So… it is NOT the same and it is NOT NOT the same?…wtf is it. AS doesn’t seem to know what it is.
Ok, bear with me a second. The following:
trace(”200.83″ == 200.83) //true
trace(”200.83″ != 200.83) //false
…that seems alright
At this point, I tried to make some sense of it and applied the following rules….
If a string has one decimal….it turns out to work fine…
If a string has two decimals….it only works fine if the last digit is uneven…
The next thing to check is a string with three decimals and…there seems to be a different stupid rule here…check…
trace("200.811" == 200.811) //true
trace(”200.811″ != 200.811) //false
trace(”200.812″ == 200.812) //true (should be false according to last “rules”)
trace(”200.812″ != 200.812) //false
Hmmm, it seems that we can erase the above rules from our head. It works fine at this point…what is that about?
One more sample…the second decimal after decimal point is an even digit.
trace("200.821" == 200.821) //false
trace(”200.821″ != 200.821) //false
trace(”200.822″ == 200.822) //false
trace(”200.822″ != 200.822) //false
Oh my god…..the second digit after decimal point is even and it is always false?? I can’t seem to make sense of it….sorry…
I can probably go on and on with more samples (four / five / etc. decimal digits) but the point is clear….it’s fucked.
If I trace typeof, it all seems to be a Number
trace(typeof(parseFloat(”200.81″))) //number
trace(typeof(parseFloat(”200.82″))) //number
trace(typeof(parseFloat(”200.821″))) //number
Converting the String to a Number or use parseFloat doesn’t work….so don’t bother trying.
Maybe it has something (the way it parses a string to a number or visa versa) to do with rounding Numbers…see what Arno has to say…
Jaap
Posted in Actionscript 2.0, Bugs, Flash 8, Flash CS3, Flex |
6 Comments »