Number 是javascript 的基本數據類型,數字,經常使用的就是獲取幾位小數點,和把字符串轉成數字,下面咱們來介紹下number的經常使用方法javascript
let number=99; console.log(number) //99 let number1=new Number(99); console.log(number1) //[Number: 99]
console.log(2.123.toFixed()) //2 console.log(2.123.toFixed(2)) //2.12
console.log(Number.parseFloat('1.2')) //1.2 console.log(Number.parseFloat('1.233er')) //1.233 console.log(Number.parseFloat('1.233er13')) //1.233
string 爲字符串,radix 爲進制數 默認是10 console.log(Number.parseInt('0110',2)) //6 console.log(Number.parseInt('32',8)) //26 console.log(Number.parseInt('10',10)) //10 console.log(Number.parseInt('100',16)) //256 console.log(Number.parseInt('100.99')) //100
console.log(number1.valueOf()) //99
console.log(Number.isFinite(Infinity)) //false console.log(Number.isFinite('12')) //false console.log(Number.isFinite(22)) //true console.log(Number.isFinite(NaN)) //false
console.log(Number.isInteger('1')) //false console.log(Number.isInteger(true)) //false console.log(Number.isInteger(false)) //false console.log(Number.isInteger(0)) //true console.log(Number.isInteger(NaN)) //false
console.log(Number.isNaN('1')) //false console.log(Number.isNaN(NaN)) //true console.log(Number.isNaN(undefined)) //false console.log(Number.isNaN(null)) //false console.log(Number.isNaN({})) //false console.log(Number.isNaN(true)) //false
console.log(77.1234.toExponential()) //7.71234e+1 console.log(77.1234.toExponential(2)) //7.71e+1
let num=3500; console.log(num.toLocaleString()) //3,500
console.log(1.234.toPrecision()) //1.234 console.log(1.234.toPrecision(2)) //1.2
let str=10; console.log(str.toString()) //'10'
今天就到這裏,天天進步一點,但願你們喜歡,也但願喜歡的朋友點個贊,後續繼續更新...java