Number data type
Number type uses IEEE754 format for representing integers and floating-point values, which is 0.2 + 0.3
not equal to 0.5
the cause,
The most basic type of the value is a decimal integer literal format
var a = 10;
1. floating-point values
2. In
Not a Number
: Is not a number, but it is numeric
NaN features
- Any NaN values and operations return NaN
- NaN is not equal to any value, including itself NaN
console.log(NaN == NaN)
3. isNaM function
Not for detecting a variable value ( Number
) type. isNaN()
Receiving a parameter, this parameter may be any type, this function will try to convert the incoming parameter values, some values may not be directly converted into numeric values, such as string "10"
or Boolean
value is not converted into a numerical value returned true
, You can convert returnfalse
console.log( isNaN(NaN ) // true
console.log( isNaN(10 ) // false
console.log( isNaN('10') // false
console.log( isNaN('blue') // true
console.log(isNaN( true )) // false (true会被转换成1)
to sum up:
isNaN
Means:是不是非数值
is then returnedtrue
, the function converts all can beNumber
value types, returnsfalse
- When using isNaN detecting, first verifies whether the detected value of the digital type, if not, to Number () This method is based on the value converted into digital type, then detects
- Empty string, empty array, Boolean,
null
will turn into values, theisNaN
function returnsfalse
- Null object, functions,
undefined
can not be converted into a numerical value, returnstrue
4. Numerical conversion
The non-numeric values to numeric values:
- Number()
- the parseInt () rounding
- parseFloat()
Number () conversion rules:
- If it is a Boolean value,
true
andfalse
will be converted to1
and0
Number(true) // 1
Number(false) // 0
- If it is digital, but simply pass and return
Number(1) // 1
Number(100) // 100
- If the value is null, 0 is returned
Number(null) // 0
- If it is undefined, NaN is returned
Number(undefined) // NaN
- If a string:
- String contains only numbers (including government)