Number类型

toString()

返回字符串类型的数值。

可以传递一个表示基数的参数,表示返回几进制数值的字符串形式。

1 let num =12;
2 num.toString(); //"12"
3 num.toString(2); //"1100"
4 num.toString(8); //"14"
5 num.toString(10); //"12"
6 num.toString(16); //"c"

toFixed()

按照指定的小数位数返回数值的字符串表示。

1 num.toFixed(); //"12"
2 num.toFixed(1); //"12.0"
3 num.toFixed(2); //"12.00"
1 let fl=12.00915;
2 fl.toFixed(1); // "12.0" 
3 fl.toFixed(2); // "12.01"
4 fl.toFixed(3); // "12.009"
5 fl.toFixed(4); // "12.0091"

toExponential()

返回以指数表示法(也称e表示法)表示的数值的字符串形式。

1 fl.toExponential(1);  // "1.2e+1"
2 let fl2=2000000;
3 fl2.toExponential(3);  //"2.000e+6"

toPrecision()

返回固定大小格式或指数格式,接受一个参数,表示数值的所有数值的位数(不包括指数部分)

1 let num3=98;
2 num3.toPrecision(1); // "1e+2"
3 num3.toPrecision(2); // "98"
4 num3.toPrecision(3); // "98.0"

 

posted @ 2017-12-16 12:48  biubiu小希希  阅读(180)  评论(0编辑  收藏  举报