Home >Web Front-end >JS Tutorial >javascript -number type value
General introduction
JavaScript uses the IEEE754 format internally to represent numbers, so it does not distinguish between integers and floating point numbers, and is stored in the form of 64-bit floating point numbers. That is to say, inside JavaScript, there are no decimals at all. However, some operations must be completed with integers, so JavaScript sometimes converts 64-bit floating point numbers into 32-bit integers before performing operations.
Integers
JavaScript provides four representation methods for integers:
1. Binary: For values with a prefix of 0b, an error will be reported if a number other than 0 or 1 appears
2. Octal: A value with a prefix of 0o, or 0 followed by a number (0-7). If it exceeds the numerical range mentioned earlier, the first digit 0 will be ignored and treated as a decimal number
Note: Octal literals are invalid in strict mode and will cause JavaScript engines that support this mode to Throws an error
3. Hexadecimal: There is a prefix of 0x, followed by any hexadecimal digit (0~9 and A~F). The letters can be uppercase or lowercase. If the range is exceeded, an error will be reported
4. Decimal
var num2 = 0b11; console.log(num2); //3 var num2 = 0b12; console.log(num2); //报错 var num8 = 0o76; console.log(num8); //02 var num8 = 0o78; console.log(num8); //报错 var num16 = 0x2a; console.log(num16); //42 var num16 = 0x2h; console.log(num16) //报错
##Floating point number
The so-called floating point number is The number must contain a decimal point and must have at least one digit after the decimal point. Different from integers, floating point numbers can only be expressed in decimal. The accuracy of floating point numbers is far inferior to that of integers, so you have to be careful when designing operations with floating point numbers. For example:console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004 console.log(0.6/0.2); //2.9999999999999996
Scientific notation
For those very large and very small values, you can use e notation ( That is, a floating-point numerical representation expressed in scientific notation). The value expressed in e notation is equal to the value in front of e multiplied by the exponential power of 10 In the following two cases, JavaScript will automatically convert the value to scientific notation. In other cases, it will be expressed directly in literal form. . 1. There are more than 21 digits before the decimal pointconsole.log(1234567890123456789012);// 1.2345678901234568e+21 console.log(123456789012365648787); //123456789012365660000
console.log(0.0000006);//6e-7 console.log(0.000006); //0.000006
Value range
Due to memory limitations, ECMAScript cannot save all value, so there is a maximum value and a minimum value The minimum value is stored in Number.MIN_VALUE, this value is 5e-324The maximum value is stored in Number.MAX_VALUE, this value is 1.7976931348623157e+308console.log(Number.MIN_VALUE) //5e-324 console.log(Number.MAX_VALUE); //1.7976931348623157e+308
var result = Number.MAX_VALUE + Number.MAX_VALUE; console.log(isFinite(result)); //false
Special values
1, +0 and -0
These two 0s are equivalent in the case of large logarithms-0 === +0; //true 0 === -0; //true 0 === +0; //true
1/-0 == 1/+0; //false
2, infinity
Infinity means "infinity" and is used to represent two scenarios. One is that a positive value is too large, or a negative value is too small to be represented; the other is that a non-zero value is divided by 0 to get Infinity.Math.pow(2,Math.pow(2,100));//Infinity 1/0;//Infinity
* Infinity;//Infinity - Infinity;//-Infinity + Infinity;//Infinity / Infinity;//0 Infinity / 2;//Infinity Infinity * Infinity;//Infinity Infinity - Infinity;//NaN Infinity + Infinity;//Infinity Infinity / Infinity;//NaN
3. NaN
This value indicates that an operand that was supposed to return a value did not return a value. Case NaN is not equal to any value, including itself, and any operation involving NaN returns NaN5 - 'x'; //NaN Math.acos(2); //NaN 0 / 0; //NaN NaN == NaN;//false NaN == Infinity;//false
typeof NaN; //number
isNaN('Hello') // true // 相当于 isNaN(Number('Hello')) // true
function isNaN(value){ return value != value; }
Number system conversion
There are 3 functions that can convert non-numeric values into Numeric values: Number(), parseInt(), and parseFloat(). Number() can convert any type of value into a numerical value, while parseInt() and parseFloat() are only used to convert strings to numbersNumber()
Conversion rules: 1. If it is a Boolean value, true and false will be converted to 1 and 0 respectively2. If it is a null value, 0# will be returned. ##3、如果是undefined,返回NaN
4、如果是字符串,遵循以下规则:
(1)若字符串只包含十进制或十六进制数字,则转成十进制的数字
注意:Number()不识别八进制数字的字符串,会按照十进制数字处理
字符串'1.2.'不会报错,但数字1.2.会报错
(2)若字符串为空字符串或空格字符串,则转成0
(3)其他情况的字符串,则转成NaN
Number(true) //1 Number(null) //0 Number(undefined) //NaN Number("0123") //123 Number("0x123") //291 Number("0.2") //0.2 Number("") //0 Number("asd") //NaN
parseInt()
parseInt()专门用于把字符串转换成整数。在转换字符串时,会忽略字符串前面的空格,直到找到第一个非空格字符。如果第一个字符不是数字字符或者负号,parseInt()就会返回NaN。如果是,则继续解析,直到解析完成或者遇到非数字字符
console.log(parseInt(' 123.8px'));//123 console.log(parseInt(' 123.8 '));//123 console.log(parseInt(' -123.8px'));//-123 console.log(parseInt('a123.8px'));//NaN console.log(parseInt('0 123.8px'));//0
注意:在ECMAScript5中,parseInt()已经不具备解析八进制的能力。例如八进制“070”,会忽略前面的“0”,得到十进制的70
为了消除在使用parseInt()函数时可能导致的上述困惑,可以为这个函数提供第二个参数:转换时使用的基数(多少进制)
parseInt("070") //70 parseInt("070",8) //56
parseFloat()
parseFloat()专门用于字符串转换浮点数。同样地,解析时会忽略字符串前面的空格,直到找到第一个非空格字符,然后一直解析到字符串末尾或一个无效的浮点数字字符为止
console.log(parseFloat(' 0123.px'));//123 console.log(parseFloat(' 123.px'));//123 console.log(parseFloat(' 123.1px'));//123.1 console.log(parseFloat(' 123.1.2px '));//123.1 console.log(parseFloat(' -123.0px'));//-123 console.log(parseFloat('.123.1px'));//0.123 console.log(parseFloat('0 123px'));//0
注意:parseFloat()只解析十进制,所以十六进制的字符串始终会被转换成0。因此也没有第二个参数用来指定基数
parseFloat("0xA") //0
注意:Number('')的结果是0,parseInt('')和parseFloat('')的结果是NaN
更多Number类型值相关文章请关注PHP中文网!