Three methods of JS numerical conversion

Source: Internet
Author: User
Tags number sign

JS non-numeric conversion to a numeric value of three of the notation is number (), parseint (), parsefloat ().

Number () is the most complex. The rules are as follows:

1. Direct numerical output;

Console.log (Number (' 11 '));//output 11;

2.Boolean value, true converted to 1,false converted to 0;

Console.log (Number (true));//output 1;
Console.log (number (false));//output 0;

3.null Convert to 0,undefined convert to Nan

Console.log (number (null));//output 0;
Console.log (number (undefined));//Output nan;

4. There are many more rules for string conversions:

(1) The string contains only numbers and is converted to decimal, ignoring the preceding 0, for example

Console.log (Number (' 011 '));//output 11;

(2) The string contains a valid floating-point format, which is converted to decimals and also ignores the preceding 0, for example

Console.log (Number (' 000.11 '));//output 0.11;

(3) The string contains a valid hexadecimal format and is converted to a decimal format value output of equal size

Console.log (Number (' 0xa '));//output 10;

(4) The string is empty, contains no characters, and is converted to 0

Console.log (Number ("));//output 0;

(5) A string other than the above string, converted to Nan

Console.log (Number (' hello! ')); /Output Nan;

The conversion rules for number () are indeed somewhat complex. And in some cases it may not be the result we want. Usually we use parseint () to convert the values.

parseint () does not convert the format of a non-numeric pattern. It ignores whitespace in front of the string until a non-whitespace character is found:

1. If the character is not a number sign or minus sign, it returns Nan

Console.log (parseint ('   hello! ')); /Output Nan;

An empty string returns Nan, which differs from number ().

Console.log (parseint ("));//Output nan;

If it is a number sign or minus sign, it will continue parsing the second until all or a non-numeric symbol character is encountered

Console.log (parseint (' 123hehe '));//output 123;

Numbers that follow non-characters are also ignored

Console.log (parseint (' 123hehe123 '));//output 123;

The decimal point is a non-numeric symbol character that causes parsing to abort.

Console.log (parseint (' 12.3 '));//Output 12

If the first non-whitespace character is a number, parseint () can parse various integer formats, including octal, decimal, and hexadecimal

Console.log (parseint (' 070 ')),//es3 output 56 (octal), ES5 output 70 (decimal), Console.log (parseint (') ');//70 (decimal) ; Console.log (parseint (' 0xa '));//10 (hex);

It can be seen that ES3 and es5 have differences in parsing octal, ES5 has no ability to parse octal. So it is necessary to use the second parameter of parseint ()

Console.log (parseint (' 070 ', 8));//Output 56;console.log (parseint (' 070 ', ten));//Output 70;console.log (parseint (' + ', 8);//Output 56;console.log(' parseint ');//Output 70;console.log (parseint (' 0xa ', 16)) ;//output 10;

If you specify 16 as the second argument, the preceding 0x can be omitted, and if not specified, it cannot be omitted, otherwise Nan is returned

Console.log (parseint (' a ', +));//Output 10;console.log (parseint (' a '));//Output nan;

It is recommended to use parseint () to add a conversion base at any time.

parseint () cannot convert floating-point values, we use parsefloat () to solve.

Parsefloat () is similar to parseint (), and will begin parsing from the first non-empty string until parsing is complete, or it encounters a non-floating-point numeric format (difference one), but it ignores all previous 0, he does not convert the cardinality, only the decimal value (difference two)

Console.log (parsefloat (' 123hehe '));//Output 123;console.log (parsefloat (' 123hehe '));//Output 123;console.log (Parsefloat (' 01.2.3 '));//Output 1.2;console.log (parsefloat (' 0xa '));//Output 0

Note that if the string can be resolved to an integer, 0 after the decimal point does not output

Console.log (parsefloat (' 1.000 '));//output 1;

Three methods of JS numerical conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.