JavaScript numeric conversion summary and javascript numeric Summary

Source: Internet
Author: User

JavaScript numeric conversion summary and javascript numeric Summary

In JavaScript, there are three ways to convert values:


1. Number (param) function: param can be used for any data type.


1.1 param is a Boolean value. true and false are converted to 1 and 0 respectively;

1.2 param is a numerical value, but it is simple to pass in and return

1.3 param is null and undefined, and 0 and NaN are returned respectively.

1.4 param is a string and follows the following rules:


1.4.1 if the string contains only numbers, it is converted to decimal. Leading 0 is ignored;

1.4.2 if the string contains a valid floating-point number format, the corresponding floating-point value is returned, leading 0 is ignored;

1.4.3 if the string contains a valid hexadecimal value, return an equal value in decimal format.

1.4.4 if the string is null, 0 is returned.

1.4.5 if the string contains characters other than the preceding format, NaN is returned.


1.5 If param is an object, the valueOf () method is called and the string value is converted according to the previous rules. If NaN is returned, the toString () method is called, convert the returned string value according to the preceding rules again.


1.6 example:

<span style="font-family:Microsoft YaHei;font-size:18px;">var num1 = Number("hello");  //NaNvar num2 = Number("");  //0var num3 = Number("00022");  //22var num4 = Number(true);  //1</span>


2. parseInt (param): converts a string to an integer. param is a string type.


ParseInt () ignores the spaces before the string until the first non-space character S is found. If S is not a number or a negative number, NaN (that is, parseInt () returns NaN for null characters, NOTE: If Number () is null, 0 is returned. If S is a Number, parseInt () will continue to parse the next character until all characters are parsed or a non-numeric character is encountered. ParseInt () supports parsing the octal and hexadecimal formats.

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> var num1 = parseInt ("1234 blue "); // 1234var num2 = parseInt (""); // NaNvar num3 = parseInt ("22.5"); // 22var num4 = parseInt ("070"); // octal, convert to 56 in decimal format </span>


3. parseIFloat (param): converts a string to a floating point. param is a string.


Similar to parseInt, parseFloat () is parsed from the first character until all characters are parsed or a non-floating-point character is encountered. The first decimal point is valid, but the second decimal point is invalid. This function can only parse the decimal number, because it will always ignore the leading 0.

<span style="font-family:Microsoft YaHei;font-size:18px;">var num1 = parseFloat("1234blue"); // 1234var num2 = parseFloat("0xf6"); // 0var num3 = parseFloat("22.5"); // 22.5var num4 = parseFloat("22.5.4"); //22.5var num5 = parseFloat("3.125e7"); // 31250000</span>



How does one convert a string to a numeric value in javascript?

Number ('3 ');
 
How does one convert a string to a numeric value in javascript?

ParseInt function parse ("123", 10); // Result 123, the second parameter is the number of strings in the hexadecimal parseFloat (s) parseFloat ("123.456 "); // returns a value of 123.456.

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.