The content of this article is about how to convert numerical values in JavaScript? The summary of JavaScript numerical conversion methods has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The following three functions convert non-numeric values into numeric values: Number(), parseInt() and parseFloat()
Boolean The value true will be converted to 1 and false will be converted to 0
var correct = true Number(correct) 1 var error = false Number(error) 0
What numeric value is passed in will be returned
var sales = 10 Number(sales) 10
null value, return 0
var arg = null Number(arg) 0
undefined, return NaN
var vara = undefined Number(vara) NaN
String, follow the following rules:
If the string contains only numbers (including the case with a positive or negative sign in front), it will be converted into a decimal value, that is, " 1"
will become 1, "123" will become 123, and "011" will become 11 (note: leading zeros are ignored)
var str = '1' Number(str) 1 var str1 = '011' Number(str1) 11 var str2 = '+1' Number(str2) 1 var str3 = '-001' Number(str3) -1 var str4 = '-121' Number(str4) -121
If the string contains a valid Floating point format, such as "1.1", convert it to the corresponding floating point value (similarly,
leading zeros will also be ignored)
var str = '1.1' Number(str) 1.1 var str1 = '+1.1' Number(str1) 1.1 var str2 = '-01.1' Number(str2) -1.1
If the string contains valid hexadecimal format, such as "0xf", then convert it to a decimal integer of the same size
Numerical value
If the string is empty (does not contain any characters), then convert it to 0
var str = '' Number(str) 0
If the string contains characters other than the above format, convert it to NaN
var str = 'adfsfdsa' Number(str) NaN
If it is an object, call the valueOf() method of the object, and then follow The previous rule transforms the returned value. If the result of the conversion is NaN, call the object's toString() method, and then convert the returned string value again according to the previous rules
Since the Number() function is more complicated and unreasonable when converting strings, the
parseInt() function is more commonly used when processing integers. When the parseInt() function converts a string, it depends more on whether it conforms to the numerical pattern. It ignores spaces preceding the
string until it finds the first non-space character. If the first character is not a numeric character, parseInt()
will return NaN; that is, using parseInt() to convert an empty string will return NaN (Number() returns 0 for null characters). If
the first character is a numeric character, parseInt() will continue to parse the second character until all subsequent characters are parsed or
a non-numeric character is encountered. For example, "1234blue" will be converted to 1234, because "blue" will be completely ignored. Similarly, "22.5"
will be converted to 22 because the decimal point is not a valid numeric character.
指定基数会影响到转换的输出结果。例如: var num1 = parseInt("10", 2); //2 (按二进制解析) var num2 = parseInt("10", 8); //8 (按八进制解析) var num3 = parseInt("10", 10); //10 (按十进制解析) var num4 = parseInt("10", 16); //16 (按十六进制解析)
**多数情况下,我们要解析的都是十进制数值,因此始终将 10 作为第二个参数是 非常必要的。**
Similar to the parseInt() function, parseFloat() also parses each character starting from the first character (position 0). And
is also parsed until the end of the string, or until an invalid floating-point numeric character is encountered. In other words, the
th decimal point in the string is valid, but the second decimal point is invalid, so the string after it will be ignored. For example,
"22.34.5" will be converted to 22.34.
In addition to the valid first decimal point, the second difference between parseFloat() and parseInt() is that it always ignores leading
zeros. parseFloat() recognizes all floating-point numerical formats discussed previously, including decimal integer formats. However, strings in the hexadecimal format
will always be converted to 0. Since parseFloat() only parses decimal values, it has no use for specifying the base number with the second parameter. One last thing to note: if the string contains a number that can be parsed as an integer (no decimal point, or
after the decimal point are all zeros), parseFloat() will return an integer.
var num1 = parseFloat("1234blue"); //1234 (整数) var num2 = parseFloat("0xA"); //0 var num3 = parseFloat("22.5"); //22.5 var num4 = parseFloat("22.34.5"); //22.34 var num5 = parseFloat("0908.5"); //908.5 var num6 = parseFloat("3.125e7"); //31250000
JavaScript Exploration: Using parseInt() for numerical conversion
The above is the detailed content of How to convert numerical values in javascript? Summary of methods for javascript numerical conversion. For more information, please follow other related articles on the PHP Chinese website!