首页 > Web开发 > 详细

js截取小数点后面2位

时间:2018-09-21 00:22:45      阅读:202      评论:0      收藏:0      [点我收藏+]
1.substr
var str = "Hello world!";
document.write(str.substr(3));
输出:lo world!

var str = "Hello world!";
document.write(str.substr(3,7));
输出:lo worl


2.toFixed(四舍五入)
var num = new Number(12.38);
document.write(num.toFixed(1));
输出:12.4
3.floor(向下取整) Math.floor(0.60); 结果:0 Math.floor(0.40); 0 Math.floor(5); 5 Math.floor(5.1); 5 Math.floor(-5.1); -6 Math.floor(-5.9); -6 3.ceil(向上取整) Math.ceil(0.60); 结果:1 Math.ceil(0.40); 1 Math.ceil(5); 5 Math.ceil(5.1); 6 Math.ceil(-5.1); -5 Math.ceil(-5.9); -5 4.round(四舍五入) Math.round(0.60); 结果:1 Math.round(0.40); 0 Math.round(5); 5 Math.round(5.1); 5 Math.round(-5.1); -5 Math.round(-5.9); -6 使用floor配合*100/100来实现向下取整截取小数点后两位 var num = 22.13758; console.log(Math.floor(num*100)/100); console.log(Math.ceil(num*100)/100); console.log(Math.round(num*100)/100); console.log(num.toFixed(2)); 输出:22.13 22.14 22.14 22.14

 

js截取小数点后面2位

原文:https://www.cnblogs.com/QW-lzm/p/9684084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!