Search code examples
actionscript-3flash-cs5

Action Script string to number


I have a problem with following statement

trace(Number("1/2")) //output NaN

but

trace(Number("1.2")) //output 1.2

So, I am bit confused as why the first statement doesn't gives correct result?


Solution

  • It probably expects the value to be a number already, not a calculation. Try to parse this string: "1+2". It'll most likely result in NaN as well.


    Edit: I've run a test

    Number("1.2") = 1.2
    Number("1+2") = NaN
    Number("1/2") = NaN
    

    So, as I said, the Number() constructor expects a number, not a calculation.