JavaScript numeric conversion (number,parseint,parsefloat)

Source: Internet
Author: User

PS: Hurry up and take a look at it ~


* There are 3 functions to convert non-numeric values to numeric values: Number(), parseint (), parsefloat ()
* The first function, the transformation function number () can be used for any data type, while the other two functions are specifically used to convert a string to a numeric value.
* These three functions will return different results for the same input;
*
*


The conversion rules for the * number () function are as follows:
* 1,boolean value, True and false will be converted to 1 or 0 respectively;
* 2, number, simple incoming and return;
* 3,null, return 0;
* 4,undefined, return nan;
* 5, String:
* A, if the string contains only numbers (including the case with a preceding or minus sign), it is converted to a decimal value, which is "1" "1, and" 011 "11 (note: The preceding 0 is ignored);
* B, if the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (again, the preceding 0 is ignored)
* C, if the string contains a valid hexadecimal, such as "0xf", it is converted to the same size decimal integer value;
* d, if the string is empty (does not contain any characters), it is converted to 0;
* E, if the string contains characters other than the above format, it is converted to Nan;
* 6, if it is an object, call the object's valueof () method, and then convert the returned value according to the previous rule. If the result of the conversion is Nan, the object's ToString () method is called, and then the returned string value is converted again according to the previous rule.
*

Console.log (Number ("Hello world!")) NaN
Console.log (Number (")"); 0
console.log (number (' 00001111 ')); 1111
Console.log (number (true));//1


* parseint () function conversion
* This function is more to see if it conforms to the numeric pattern when converting a string. It ignores the spaces in front of the string until the first non-empty string character is found.
* If the first character is not a number or minus sign, it will return nan;
* If the first character is a numeric character, it will continue parsing the second characters until the subsequent characters are resolved or a non-numeric character is encountered.
* For example: "1234andy" will be converted to 1234; "22.5" will be converted to



Console.log (parseint (' 1234andy ')); 1234
Console.log (parseint (")); NaN
Console.log (parseint (' 0xA ')); 10 (hex)
Console.log (parseint (22.5)); A
Console.log (parseint (' 070 ')); 56 (octal)//Note: ES3 is considered to be 56 (octal) when parsing a string of octal letters using parseint (), ES5 is considered 70 (decimal)
Console.log (parseint (' 70 ')); 70 (decimal)
Console.log (parseint (' 0xf ')); 15 (hex)

//parseint () when two parameters are passed in
Console.log (parseint (' 10 ', 2))//by binary parsing
Console.log (parseint (' 10 ', 8))//by octal parsing
Console.log (parseint (' 10 ', 10))//By decimal resolution
Console.log (parseint (' 10 ', 16))//hexadecimal parsing


* parsefloat ()
* This function also parses each character starting from the first character (position 0). It is also parsed until the end of the string, or until an invalid floating-point numeric character is encountered.
* That is, the first decimal point in the string is valid, and the second decimal point is invalid, so the string after it is ignored;
* This function parses only the decimal value because it does not have a second parameter that specifies the usage of the cardinality.
*

Console.log (parsefloat (' 1234blue ')); 1234
Console.log (parsefloat (' 0xA ')); NaN
Console.log (parsefloat (' 22.5 ')); 22.5
Console.log (parsefloat (' 22.34.5 '));//22.34
Console.log (parsefloat (' 0908.5 '));//908.5
Console.log (parsefloat (' 3.125e7 '));//31250000

JavaScript numeric conversion (number,parseint,parsefloat)

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.