In JavaScript, there are generally three ways to convert a numeric value:
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 a numeric value that simply passes in and returns
1.3 param is null and undefined, returning 0 and Nan respectively
1.4 param is a string that 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, the decimal value of equal size is returned
1.4.4 If the string is empty, 0 is returned
1.4.5 If the string contains characters other than the above format, the Nan is returned
1.5 param is an object, the ValueOf () method is called, the string value is returned in accordance with the previous rule transformation, and if Nan is returned, the ToString () method is called, and the string value is returned in accordance with the previous rule transformation .
1.6 Example:
var num1 = number ("Hello"); // NaN var num2 = number (""); // 0 var num3 = number ("00022"); // A var num4 = number (true); // 1</span>
parseint (param): Converts a string to an integer, param is a string type.
parseint () ignores whitespace in front of the string until it finds the first non-whitespace character s; if s is not a number or minus sign, it returns Nan (that is, parseint () returns Nan to the null character, note that number () returns 0 for null), and if s is a digit, parseint () Continues parsing the next character until all characters are parsed or a non-numeric character is encountered. parseint () supports parsing of octal and hexadecimal
var num1 = parseint ("1234blue"); // 1234 var num2 = parseint (""); // NaN var num3 = parseint ("22.5"); // A var num4 = parseint ("070"); // octal, converted to decimal 56</span>
Parseifloat (param): Converts a string to a floating-point number, param is a string type.
Similar to parseint, parsefloat () begins parsing from the first character until all characters have been parsed or a non-floating-point number is encountered. The first decimal point is valid, but the second one is invalid, and the function can only parse the decimal number because it always ignores the leading 0.
var // 1234 var // 0 var // 22.5 var // 22.5 var // 31250000</span>
Search Number:ruixin, or scan the QR code below, follow the blog public number, irregular brain the Internet: