convert string to a number

20,898 views
Skip to first unread message

Ananth Raghuraman

unread,
Apr 24, 2009, 12:06:47 PM4/24/09
to Prototype & script.aculo.us
Need prototype function to convert object/string/number to number. Please help.

Hector Virgen

unread,
Apr 24, 2009, 2:17:25 PM4/24/09
to prototype-s...@googlegroups.com
Can you use the native parseInt() function? or parseFloat()?

var num = parseInt("52");
console.log(num); // int 52


-Hector

RobG

unread,
Apr 25, 2009, 12:02:20 AM4/25/09
to Prototype & script.aculo.us


On Apr 25, 2:06 am, Ananth Raghuraman <araghuram...@gmail.com> wrote:
> Need prototype function to convert object/string/number to number. Please
> help.

Without a more concise specification for what the object/string/number
might be, you will end up with a large function that works for a few
scenarios invented by the author and fails for anything else.

It doesn't make sense to have a function to convert a number to a
number.


--
Rob

Ananth Raghuraman

unread,
Apr 25, 2009, 1:37:18 AM4/25/09
to prototype-s...@googlegroups.com
Thanks for pointing me to the native functions!
In the meantime I also found 1.8 has the Number object/function.
I just did Number(mynumberstring) to convert mynumberstring to a number.
I guess Number also accepts an object or another Number as argument..

pradeep

unread,
Apr 25, 2009, 1:52:36 AM4/25/09
to Prototype & script.aculo.us
Dear Ananth,

you can also use..

Math.abs(string); --------> to get the number

which is one of the prototypes method.


However, the beauty of the javascript is that ....it automatically
typecaste's the varible declared to number or string..
depending on the usage ..

could you tell me , where it went wrong (i mean typcasting) ??

Ananth Raghuraman

unread,
Apr 25, 2009, 3:13:14 AM4/25/09
to prototype-s...@googlegroups.com
@Pradeep:

I never really tried typecasting or the native functions because I didnt know about them.
I actually started hardcode Javascript coding with the Prototype/Scriptaculous frameworks so I am used to
using Prototype functions everywhere when there is a choice.

RobG

unread,
Apr 26, 2009, 8:18:32 AM4/26/09
to Prototype & script.aculo.us


On Apr 25, 3:37 pm, Ananth Raghuraman <araghuram...@gmail.com> wrote:
> Thanks for pointing me to the native functions!
> In the meantime I also found 1.8 has the Number object/function.

The Number constructor is a built-in ECMAScript function. When called
as a function, it does type conversion:

var num = '1';
// Convert to number
num = Number(num);

How it works is covered by Section 15.7.1.1 of the ECMAScript language
specification.

> I just did Number(mynumberstring) to convert mynumberstring to a number.

It is faster to use unary +:

var num = '1';
// Convert to number
num = +num;

Also note that any arithmetic operation on a string other than + will
always perform type conversion:

var a = '5';
alert( a * a); // 25

> I guess Number also accepts an object

Yes, it's explained in section 9.3 on type conversion. The result of
calling Number(object) is, more or less, Number(object.toString()),
however read the ECMAScript specification for a full explanation.

> or another Number as argument..

Yes, but calling Number(number) simply returns the number (Sec. 9.3).


--
Rob
Reply all
Reply to author
Forward
0 new messages