js Number 标准方法

  • number.toExponential(fractionDigits)

    toExponential方法把这个number转换成一个指数形式的字符串。可选参数fractionDigits控制其小数点后的数字位数。它必须在0~20:

Math.PI.toExponential(0); //3e+0
Math.PI.toExponential(2); //3.14e+0
Math.PI.toExponential(7); //3.1415927e+0
Math.PI.toExponential(16);//3.1415926535897930e+0
Math.PI.toExponential();  //3.141592653589793e+0
  • number.toFixed(fractionDigits)

    toFixed方法把这个number转换成为一个十进制数形式的字符串。可选参数fractionDigits控制其小数点后的数字位数。它的值必须在0~20,默认为0:

Math.PI.toFixed(0); //3
Math.PI.toFixed(2); //3.14
Math.PI.toFixed(7); //3.1415927
Math.PI.toFixed(16);//3.1415926535897930
Math.PI.toFixed();  //3
  • number.toPrecision(precision)

    toPrecision方法把这个number转换成为一个十进制数形式的字符串。可选参数precision控制数字的精度。它的值必须在0~21:

Math.PI.toPrecision(2); //3.1
Math.PI.toPrecision(7); //3.141593
Math.PI.toPrecision(16);//3.141592653589793
Math.PI.toPrecision();  //3.141592653589793
  • number.toString(radix)

    toString方法把这个number转换成为一个字符串。可选参数radix控制基数。它的值必须在2~36。默认的radix是以10为基数的。radix参数最常用的是整数,但是它可以用任意的数字。

    在最通常的情况下,number.toString()可以更简单地写为String( number ):

Math.PI.toString(2); //11.001001000011111101101010100010001000010110100011
Math.PI.toString(8); //3.1103755242102643
Math.PI.toString(16);//3.243f6a8885a3
Math.PI.toString();  //3.141592653589793
※摘抄自《JavaScript语言精粹(修订版)》

猜你喜欢

转载自blog.csdn.net/baidu_24563939/article/details/80512054