js数值知识点


字符串数值取整并转为相对进制
1
2
3
Number.parseInt('12.643') //12
Number.parseInt(12.643) //12
Number.parseInt(12.643, 8) //10,因为是8进制
字符串数值取浮点数
1
2
Number.parseFloat('12.345') // 12.345
Number.parseFloat(12.345) // 12.345
保留几位小数

Number.prototype.toFixed()保留数字中小数点后几位,默认为小数点后0位,不修改原始值,返回值为字符串

1
2
3
4
12.34.toFixed() // '12'等价于toFixed(0),并四舍五入
12.67.toFixed() // '13'
12.67.toFixed(1) // '12.7'
12.67.toFixed(5) // '12.67000'

数字转为字符串

Number.prototype.toString()

1
2
12.34.toString() // '12.34'
12.34.toString(8) // '14.2560507534121727'因为转为8进制了