1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

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).

A. Tip

The process of converting PDF to text doesn’t have to be a tough process. With the right software, you can convert pdf to doc safely, cheaply and easily.

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 results):
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)
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 cpmcatenates strings.

Tags: , , ,

Popularity: 44% [?]


Information and Links




moneybookers
Acne Freedom in 72 Hours

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: