JS中的toFixed,toExponential,toPrecision

toFixed():根据小数点后的指定位数,将数字转换为字符串,它不用指数计数法。

toExponential():根据小数点后指定位数,将数字转化为指数形式字符串。小数点前面只有一位,小数点后面的位数,有参数指定。

toPrecision():根据有效数字位数,将数字转化为字符串。如果有效数字位数少于数字整数部分,则表示成指数形式字符串。

以上三个方法,都遵循“四舍五入”或者补0的原则。

下面是一些例子:

var num = 1234567.8999;
console.log("num.toFixed(0):" + num.toFixed(0));
console.log("num.toFixed(2)" + num.toFixed(2));
console.log("num.toFixed(5)" + num.toFixed(5));
console.log("num.toFixed(12)" + num.toFixed(12));

console.log("num.toExponential(3):" + num.toExponential(3));
console.log("num.toExponential(0):" + num.toExponential(0));

console.log("num.toPrecision(1):" + num.toPrecision(1));
console.log("num.toPrecision(5)" + num.toPrecision(5));
console.log("num.toPrecision(12)" + num.toPrecision(12));

对应下面的输出:

转载于:https://my.oschina.net/andyzzh1314/blog/1858643

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值