Three ways to summarize JavaScript numeric conversion _javascript tips

Source: Internet
Author: User

In JavaScript, numeric conversions typically have three different ways:

One, number (param) function: param can be used for any data type

1.1 Param is a Boolean value, and True and false are converted to 1 and 0, respectively;

1.2 param is numeric, just simple to pass in and return

1.3 param are null and undefined, respectively, returning 0 and Nan

1.4 param is a string followed by the following rules:

1.4.1 If the string contains only numbers, the conversion is decimal, leading 0 is ignored;

1.4.2 If a valid floating-point number format is included in the string, the corresponding floating-point value is returned, leading 0 is ignored;

1.4.3 If the string contains valid hexadecimal, the decimal value of equal size is returned

1.4.4 returns 0 if the string is empty

1.4.5 returns Nan if the string contains characters other than the above format

1.5 param is an object, calls the ValueOf () method, returns a string value according to the preceding rule transformation, and, if it returns Nan, calls the ToString () method and returns the string value again according to the preceding rule transformation.

1.6 Example:

<span style= "Font-family:microsoft yahei;font-size:18px;" >var NUM1 = number ("Hello"); NaN 
var num2 = number (""); 0 
var num3 = number ("00022");//22 
var num4 = number (true);//1</span>

Ii. parseint (param): Converts a string to an integer, param is a string type.

parseint () ignores the space before the string until it finds the first non-space character s, and if S is not a number or minus sign, returns Nan (that is, parseint () returns Nan to the null character, noting that number () returns 0 for null), if S is numeric, parseint () Continues parsing the next character until all characters are parsed or a non-numeric character is encountered. parseint () support for octal, hexadecimal parsing

<span style= "Font-family:microsoft yahei;font-size:18px;" >var NUM1 = parseint ("1234blue"); 1234 
var num2 = parseint (""); NaN 
var num3 = parseint ("22.5");//22 
var num4 = parseint ("070");//Eight binary, convert to decimal 56</span>

Parseifloat (param): Converts a string to a floating-point number, param is a string type.

Like parseint, parsefloat () resolves from the first character until all characters have been parsed or a non floating-point number character has been encountered. The first decimal point is valid, but the second is invalid, and the function can only resolve decimal numbers because it always ignores leading 0.

 <span style= "Font-family:microsoft yahei;font-size:18px;" >var NUM1 = parsefloat ("1234blue"); 1234 var num2 = parsefloat ("0xf6"); 0 var num3 = parsefloat ("22.5"); 22.5 var num4 = parsefloat ("22.5.4"); 22.5 var num5 = parsefloat ("3.125e7"); 31250000</span> 
Related Article

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.