美文网首页
JS常用的保留小数后两位方法

JS常用的保留小数后两位方法

作者: 仰望天空的人 | 来源:发表于2018-11-29 15:35 被阅读105次
 var num =2.446242342;
num = num.toFixed(2); // 输出结果为 2.45



Math.floor(15.7784514000 * 100) / 100 // 输出结果为 15.77


Number(15.7784514000.toString().match(/^\d+(?:\.\d{0,2})?/)) 
// 输出结果为 15.77,不能用于整数如 10 必须写为10.0000


var a = "23.456322";
var aNew;
var re = /([0-9]+.[0-9]{2})[0-9]*/;
aNew = a.replace(re,"$1");
alert(aNew);





var num=22.127456;
alert( Math.round(num*100)/100);



var num = new Number(13.376954);
document.write (num.toFixed(2))







tmp ="3.376954";
result = tmp.substr(0,tmp.indexOf('.')+2);
alert(result);

相关文章

网友评论

      本文标题:JS常用的保留小数后两位方法

      本文链接:https://www.haomeiwen.com/subject/odpcqqtx.html