码迷,mamicode.com
首页 > Web开发 > 详细

JS 保留2位小数 四舍五入(小数点后面不足2位,自动用0补齐)

时间:2017-12-25 19:38:08      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:dex   pre   保留   div   param   www   fun   小数   +=   

function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
alert(‘function:changeTwoDecimal->parameter error‘);
return false;
}
var f_x = Math.round(x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf(‘.‘);
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += ‘.‘;
}
while (s_x.length <= pos_decimal + 2) {
s_x += ‘0‘;
}
return s_x;
}

参考链接:http://www.jb51.net/article/45884.htm

JS 保留2位小数 四舍五入(小数点后面不足2位,自动用0补齐)

标签:dex   pre   保留   div   param   www   fun   小数   +=   

原文地址:https://www.cnblogs.com/Neilisme/p/8110443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!