4

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?

ketan
  • 19,129
  • 42
  • 60
  • 98
sameer jain
  • 147
  • 1
  • 1
  • 13
  • the object passed in the constructor should directly represent a double-precision floating-point number. – abhinav Jan 27 '12 at 08:40

3 Answers3

12

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.

Tim S.
  • 13,597
  • 7
  • 46
  • 72
1

You can convert strings that are made up of numerical characters into actual Number data using the Number(). The way it works is that you pass the String value to the Number(), and in turn, this will create a Number version of the String that was passed to it.

    trace(Number("1")/Number("2"));     // Output 0.5

NaN is the output because you are trying to convert the String data to be used as Number data.

You have to trace like this because "/" operator is not a number. You can only multiply or divide numbers, NOT strings. So in the act of trying to divide String data, we are implicitly coercing the values to change into Number data. We can't do that. We should explicitly convert the String data to Number data first, and then perform the arithmetic operation.

Swati Singh
  • 1,863
  • 3
  • 19
  • 40
  • Your explanation is correct concerning numbers, but a) `Number ()` is not a constructor, but a top level conversion function http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/package.html#Number() , and b) it is safer and more accurate to use `parseFloat()` or `parseInt()` to convert strings to numerical values (parses the string until the first non-numerical character, and always returns NaN, if the string can't be converted - check the table for `Number()`'s possible return values to see what I mean). – weltraumpirat Jan 27 '12 at 10:29
  • @weltraumpirat: thanx for correcting me. i have modified my answer as Number() is not a constructor. – Swati Singh Jan 27 '12 at 10:53
-1

By enclosing the value inside quotation parks you are making it an explicit string. It's like asking what is the number value of the word "this".

Not sure if this helps but remove the quotation marks and it makes sense.

trace(Number(1/2)); //output 0.5
crooksy88
  • 3,849
  • 1
  • 24
  • 30
  • The statement was input by user so I can not remove the quotation. Any other way to solve that? – sameer jain Jan 27 '12 at 09:13
  • Is your input always the same format? i.e. a fraction? Is so you could parse the string, getting the characters before the / and the ones after the / and then perform your calculation on those substrings. e.g. var str:String = "1"; var str2:String = "2"; trace(Number(str) / Number(str2)); //output 0.5 – crooksy88 Jan 27 '12 at 11:10