Number
- number.toExponential(fractionDigits)
toExponential方法把这个number转换成指数形式的字符串,可选参数fractionDigits控制其小数点后面的位数,它的值必须字啊0~20之间。
console.log(Math.PI.toExponential(0)); //3e+0
console.log(Math.PI.toExponential(3)); //3.142e+0
- number.toFixed(fractionDigits)
toFixed方法把这个number转换成一个十进制的字符串,可选参数fractionDigits控制其小数点后面的位数,它的值必须在0~20之间,默认为0。
console.log(Math.PI.toFixed(0)); //3
console.log(Math.PI.toFixed(3)); //3.142
- number.toPrecision(fractionDigits)
toPrecision方法把number转换成一个字符串,可选参数fractionDigits控制数字的精度。它的值也必须在0~20之间。
console.log(Math.PI.toPrecision(2)); //3.1
console.log(Math.PI.toPrecision(6)); //3.14159
number.toString(radix)
toString方法把这个number转换为一个字符串。可选参数radix控制基数,它的值必须在2~36之间。默认的是10。 number.toString()可以更简单的写成 String(number).
console.log(Math.PI.toString(2)); //11.001001000011111101101010100010001000010110100011 2进制
console.log(Math.PI.toString(8)); //3.1103755242102643