Javascript - convert strings to numbers

How to convert strings to numbers in JavaScript?

To convert a string to a number, use the JavaScript function parseFloat (for conversion to a floating-point number) or parseInt (for conversion to an integer).

Syntax:
parseFloat('string')
parseInt( 'string' [, base] )

The argument of parseFloat must be a string or a string expression. The result of parseFloat is the number whose decimal representation was contained in that string (or the number found in the beginning of the string). If the string argument cannot be parsed as a decimal number, the results will be different in different browsers (either 0 or NaN).

The first argument of parseInt must be a string or a string expression. The result returned by parseInt is an integer whose representation was contained in that string (or the integer found in the beginning of the string).
The second argument (base), if present, specifies the base (radix) of the number whose string representation is contained in the string. The base argument can be any integer from 2 to 36. If there is only one argument, the number base is detected according to the general JavaScript syntax for numbers. Strings that begin with 0x or -0x are parsed as hexadecimals; strings that begin with 0 or -0 are parsed as octal numbers. All other strings are parsed as decimal numbers. If the string argument cannot be parsed as an integer, the results will be different in different browsers (either 0 or NaN).

Examples (comments in each line give the conversion result):
parseFloat(’2.72lb’) // 2.72
parseFloat(’45.7′) // 45.7
parseFloat(’045.7′) // 45.7
parseFloat(’0×45.7′) // 0
parseFloat(’.8′) // 0.8
parseFloat(’0.1e4′) // 1000
parseInt(’987.27′) // 987
parseInt(’23′) // 23
parseInt(’023′,10) // 23
parseInt(’77′,8) // 63 (= 7 + 7*8)
parseInt(’077′) // 63 (= 7 + 7*8) - since the string starts with 0, the base is set to 8
parseInt(’77′,16) // 119 (= 7 + 7*16)
parseInt(’0×77′) // 119 (= 7 + 7*16)
parseInt(’099′) // 0 (9 is not an octal digit)
parseInt(’99′,8) // 0 or NaN, depending on the platform
parseInt(’0.1e6′) // 0
parseInt(’ZZ’,36) // 1295 (= 35 + 35*36)

Note:
If you prompt the user for a number, using the prompt function, what you actually get is a string that contains the “number”. This is also the case, if you look at the value of a text field on a form. This means that before you can use the number, you must convert it to a “real” number.
JavaScript will convert a string to a number for you automatically, however it may not handle it properly, especially if there is a ‘+’ operator involved. Remember the ‘+’ operator adds numbers and comcatenates strings.

To check if the result is NaN, use isNan(param) function!

Hope it helps!

Tags: , , ,

Popularity: 68% [?]


1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

Check out this video!!

Write a Comment

Take a moment to comment and tell me what you think. Some basic HTML is allowed for formatting.

*
To prove that you're not a bot, enter this code
Anti-Spam Image

Type your comment here.

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Reader Comments

Javascript - convert strings to numbers - very big thank you for this information for JAVASCRIPT :cool:

Thank you very much for the information. Lastly, I was doing stuff like this in C language. :)

Good Article

Thanks for the post

I’m relatively new to seriously using JavaScript, I had previously tried to use PHP to do everything. The biggest problem I have found I run into using JavaScript is not converting strings to numbers when I should. I can’t tell you how much time I’ve wasted because I didn’t notice 12 + 12 was adding up to 1212 in one of my programs.

Yeah, there are some things that you need to look out when switching to Javascript from Php… but, you’ll get used to it :)

thank you for this function it helps me a lot

Hi this piece of code was really great. I can use this for my web programming subject specially in text fields for our error trap assignment. Thanks for sharing this js buddy.

Hi this piece of code was really great. I can use this for my web programming subject specially in text fields for our error trap assignment. Thanks for sharing this js buddy.

In the past I used to use:
Number(”10″)
which seems to work okey.

Also, you can use the + sign to convert a string to a number (only learnt this one receintly though). Try it:
javascript:alert(typeof +”10″); // Number
javascript:alert(typeof “10″); // String