Home  >  Article  >  Web Front-end  >  Number(), parseInt() and parseFloat() numerical conversion

Number(), parseInt() and parseFloat() numerical conversion

PHP中文网
PHP中文网Original
2017-06-22 14:08:223826browse

There are three functions that can convert non-numeric values ​​into numeric values: Number(), parseInt() and parseFloat(). The first function, the conversion function Number(), can be used for any data type, while the other two functions are specifically used to convert strings into numbers. These three functions will have different results for the same input.

The conversion rules of the Number() function are as follows:

If it is a Boolean value, true and false will be converted to 1 and 0 respectively
If it is a numeric value, it is simply passed Enter and return
If it is a null value, return 0
If it is undefined, return NaN
If it is a string, follow the following rules:
If the string only contains numbers, convert it to Decimal value, level "1" will become 1, "123" will become 123, and "011" will become 11 (the preceding 0 is ignored)
If the string only contains valid floating point formats , such as "1.1", then convert it to the corresponding floating point value (similarly, leading zeros will also be ignored)
If the string only contains valid hexadecimal format, such as "0xf", then convert it Is a decimal integer value of the same size
If the string is empty (contains no characters), it is converted to 0
If the string contains characters other than the above format, it is converted to NaN
If it is an object, call the valueOf() method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, it is indeed a bit complicated to call the object's toString() to convert various data types into numerical values. Here are a few specific examples:

var num1 = Number("Hello world!"); //NaN
var num2 = Number(""); //0
var num3 = Number("000011"); //11
var num4 = Number("true"); //1

First, the string "Hello world!" will be converted to NaN because it does not contain any meaningful numeric value. Empty strings will be converted to 0. The string "000011" is converted to 11 because leading zeros are ignored. Finally, the true value is converted to 1.

Because 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 leading spaces in the string until it finds the first non-space character. If the first character is not a numeric character or a negative sign, parseInt() will return NaN; that is, using parseInt() to convert an empty string will return NaN (Number() returns 0 for an empty string). If the first character is a numeric character, parseInt() will continue parsing the second character until all subsequent characters have been parsed or a non-numeric character is encountered. For example, "123blue" would be converted to 1234 because "blue" would be completely ignored. Similarly, "22.5" will be converted to 22 because the decimal point is not a valid numeric character.

If the first character in the string is a numeric character, parseInt() can also recognize various integer formats (i.e. decimal, octal, hexadecimal). That is, if the string starts with "0x" and is followed by numeric characters, it is treated as a hexadecimal integer; if the string starts with "0" and is followed by numeric characters, it is treated as an octal Analyze by numbers.

In order to better understand the conversion rules of the parseInt() function, some examples are given below:

var num1 = parseInt("1234blue"); //1234
var num2 = parseInt(""); //NaN
var num3 = parseInt("0xA") //10(十六进制)
var num4 = parseInt("22.5"); //22
var num5 = parseInt("070"); //56(八进制)
var num6 = parseInt("70"); //(70)十进制
var num7 = parseInt("0xF") //15(十六进制)

When understanding these examples, the most important thing is to pay attention to parseInt() parsing Different ways of "070" and "70". At this time, the leading zeros in "070" indicate that this is a string in octal (not decimal) format, so the result is 56 (note that this result is different from calling the Number() function). And "70", because there is no leading zero, is converted to 70. In order to eliminate the above confusion that may occur when using the parseInt() function, ECMAScript also provides a second parameter for this function: the base used for conversion (that is, the base).

If you want to know that the value to be parsed is a string in hexadecimal format, then specifying base 16 as the second parameter can ensure the correct result, for example:

var num = parseInt("0xAF", 16); //175
Actually, if 16 is specified as the second parameter, the string can be without the preceding "0x", as shown below:

var num1 = parseInt("AF", 16); //175
var num2 = parseInt("AF"); //NaN

The first conversion in this example succeeds, but the second one fails. The difference is that the first conversion passes in the radix, explicitly telling parseInt() to parse a string in hexadecimal format; while the second conversion finds that the first character is not a numeric character, so it automatically terminates.

Specifying the base will affect the output result of the conversion. For example:

var num1 = parseInt("10", 2); //2
var num2 = parseInt("10", 8);  //8
var num3 = parseInt("10", 10); //10
var num4 = parseInt("10", 16); //16

Since not specifying the base means letting parseInt() decide how to parse the input string, to avoid incorrect parsing, we recommend explicitly specifying the base no matter what the situation - especially In the case of dealing with octal like this:

var num1 = parseInt("010"); //8
var num2 = parseInt("010", 8); //8
var num2 = parseInt("010", 10); //10

  在这个例子中,“010”会因为第二个参数不同而被转换成不同的值。第一行的转换很直观,即让parseInt()决定如何转换。由于第一个字符是 “0”而后面也是数字字符,因而parseInt()假设它是一个八进制数。实际上,parseInt()的这个默认行为域第二行转换中明确了基数行为是 一致的。第三行传入基数10,因此parseInt()就会忽略字符串中的前导零,而只解析其余的数字符。

  多数情况下,我们要解析的都是十进制数值,因此始终将10作为第二个参数是非常必要的。

  与parseInt()函数类似,parseFloat()也是从第一个字符(位置0)开始解析每个字符。而且也是一直解析到字符串末尾,或者解析 到遇见一个无效的浮点数字字符为止。也就是说,字符串中的第一个小数点是有效的,而第二个小数点是无效的,因此它后面的字符串将被忽略。举例来 说,“22.34.5”将会被转换为22.34。

  除了第一个小数点有效之外,parseFloat()与parseInt()的第二个区别在于它始终都会忽略前导零。parseFloat()可以 识别前面讨论过的所有浮点数值格式,也包括十进制整数格式。但十六进制格式的字符串始终会被转换为0。由于parseFloat()只解析十进制值,因此 它没有用第二个参数指定基数的用法。最后还要注意一点:如果字符串包含的是一个可解析为整数的数(没有小数点,或者小数点后面都是 零),parseFloat()会返回整数。以下是使用parseFloat()转换数值的几个典型示例:

var num1 = parseFloat("1234blue"); //1234
var num1 = parseFloat("0xA"); //0
var num1 = parseFloat("22.5"); //22.5
var num1 = parseFloat("22.34.5"); //22.34
var num1 = parseFloat("0908.5"); //908.5
var num1 = parseFloat("3.125e7"); //31250000

The above is the detailed content of Number(), parseInt() and parseFloat() numerical conversion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn