js中的Number方法

1.Number.toExponential(fractionDigits)

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

例如:

 1 document.writeln(Math.PI.toExponential(0));
 2 document.writeln(Math.PI.toExponential(2));
 3 document.writeln(Math.PI.toExponential(7));
 4 document.writeln(Math.PI.toExponential(16));
 5 document.writeln(Math.PI.toExponential(  ));
 6 
 7 // Produces
 8 
 9 3e+0
10 3.14e+0
11 3.1415927e+0
12 3.1415926535897930e+0
13 3.141592653589793e+0

 

 

2.number.toFixed(fractionDigits)

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

 1 document.writeln(Math.PI.toFixed(0));
 2 document.writeln(Math.PI.toFixed(2));
 3 document.writeln(Math.PI.toFixed(7));
 4 document.writeln(Math.PI.toFixed(16));
 5 document.writeln(Math.PI.toFixed(  ));
 6 
 7 // Produces
 8 
 9 3
10 3.14
11 3.1415927
12 3.1415926535897930
13 3

 

 

3.number.toPrecision(precision)

把这个number转化为一个十进制形式的字符串。可选参数控制字符精度,它的精度必须在0~21之间。例如:

 1 document.writeln(Math.PI.toPrecision(2));
 2 document.writeln(Math.PI.toPrecision(7));
 3 document.writeln(Math.PI.toPrecision(16));
 4 document.writeln(Math.PI.toPrecision(  ));
 5 
 6 // Produces
 7 
 8 3.1
 9 3.141593
10 3.141592653589793
11 3.141592653589793

 

 

4.number.toString(radix)

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

在最普通的情况下,该方法可以写成String(number)

例如:

 1 document.writeln(Math.PI.toString(2));
 2 document.writeln(Math.PI.toString(8));
 3 document.writeln(Math.PI.toString(16));
 4 document.writeln(Math.PI.toString(  ));
 5 
 6 // Produces
 7 
 8 11.001001000011111101101010100010001000010110100011
 9 3.1103755242102643
10 3.243f6a8885a3
11 3.141592653589793

 

posted on 2016-03-12 11:14  莫尤公子  阅读(3871)  评论(0编辑  收藏  举报

导航