Action Script string to number

sameer jain picture sameer jain · Jan 27, 2012 · Viewed 29.7k times · Source

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?

Answer

Tim S. picture Tim S. · Jan 27, 2012

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.