What are the methods of transferring numbers in js? I often use string * 1, but if I encounter some special situations, using this method will cause unnecessary trouble. Let's look at three common methods of digit conversion provided by js.
Number ()
1. Normal conversion of digits, can handle decimal, binary, octal and hexadecimal.
Number(0000012323) //5331 Number(0xf) //15 Number(001) //1 Number(1.1) //1.1 Number(0.11) //0.11 Floating Point Conversion Normal Number(00.11) //Report errors
2. Boolean values; other types
Number(false) //0 Number(true) //1 Number() // 0//empty return 0 Number('') //0 Number(null) //0 Number(undefined) //NaN
3. Strings
Number('0xf') //15//1. Hexadecimal formats, such as "0xf", are converted to decimal values of the same size Number('0000012323') //12323//2. Leader 0 will be ignored Number('0.11') //0.11 Number('00.11') //0.11//3. Floating-point converts normally and ignores leading 0 Number('1b1')//NaN//4. In other cases, the string returns NaN
4. Object: valueOf() and toString() methods are called first
Number({a:1}) //NaN === Number('[object Object]') Number([1]) //1 ===Number('1') Number([1,2]) //NaN === Number('1,2')
parseInt (num, type)
parseInt has two parameters, and the second parameter represents the decimal number desired for conversion. The conversion will be rounded up.
1. Digital Conversion, Converting Floating Points to Integers
parseInt(10); parseInt(10.1) //10 parseInt(10.1,2) //2 parseInt(0.23) //0 parseInt(10.111.1,10) //Error: missing) after argument list parseInt(0010.111,10) //Error: missing) after argument list
2. Boolean values; other types
parseInt(false) //NaN parseInt(null) // NaN parseInt(undefined) //NaN parseInt() //NaN parseInt('') //NaN
3. String: If the beginning of the string is not positive, negative or numeric, it returns to NaN; if it encounters non-numeric or end, it stops parsing.
parseInt('-1') //-1 parseInt('+1') //1 parseInt('a1') //NaN parseInt('12a') //12 parseInt('12,a') //12 parseInt('012,a') //12 parseInt('0.12,a') //0 parseInt('0000.12,a') //0
4. Objects: valueOf() and toString() methods are called first, then processed as strings
parseInt({a:1}) // NaN === parseInt('[object Object]') parseInt([1,1.2],2) // 1 === parseInt('1,1.2',2) parseInt([10,1.2],2) //2 === parseInt('10,1.2',2)
parseFloat()
Similar to the parseInt() function, parseFloat() parses each character from the first character (position 0). It also parses to the end of the string, or until an invalid floating-point numeric character is encountered.
parseFloat() only parses decimal, so it does not have a second parameter to specify the use of cardinality
1. Numbers
parseFloat(0.2222) //0.2222 parseInt(0xf,10) //15
2. Boolean; other types
parseFloat() //NaN parseFloat('') //NaN parseFloat(false) //NaN parseFloat(null) //NaN parseFloat(undefined)//NaN
3. Strings
parseFloat('00.123') //0.123 parseFloat('00.123aa') //0.123 // / Only the first digit is taken parseFloat('-0.123aa') //-0.123 parseFloat('-123.1.3') //- 123.1// encounter the second decimal point when non-digital processing parseFloat('-1.0') //- Returns an integer when the decimal point is zero
4. Objects: Ibid.
parseFloat([1.33,1.2]) //1.33 parseFloat({a:1}) //NaN
IV. DIFFERENCE
Number(), parseInt(), and parseFloat() can be summarized as follows:
1. Parameters: Number() and parseFloat have only one data source parameter; () parseInt() has one more binary processing parameter.
2. Processing floating-point numbers: parseInt() is rounded; Number() and parseFloat() are not.
3. Processing mechanism: Number returns NaN when it finds non-numbers (excluding positive and negative signs) while parseInt does not return NaN as long as it finds numbers at the beginning of the data.