宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

Javascript保留小数点后指定位数

2017年1月5日 718点热度 0人点赞 0条评论

问题描述:

var num = 5.5555;
保留小数点后2位


解决方法:
1. 使用 Math.round()
  Math.round() 返回与给出的数值表达式最接近的整数
  保留2位小数,可以先乘100,Math.round() 处理后再除以100
  var result=Math.round(num*100)/100;  //result =  5.56


2. JS1.5以上可以使用 toFixed(x),保留小数点后 x 位
   var result = num.toFixed(2);    //result = 5.56

标签: 暂无
最后更新:2017年1月5日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2