IDEA中HTML页面的数据的值,四舍五入保留一位小数或者保留两位小数

时间:2020-07-29 15:31:43   收藏:0   阅读:93
html页面:

{
field: ‘suppliersLevel‘,
align: ‘center‘,
title: ‘供应商级别‘,


formatter:function(value, row, index){

if(value==0){
return value;
}else{
var keepOne = jsHelp.keepOneContainZero(value);
return jsHelp.keepOneContainZero(value);
}


}
}
js页面中
keepOneContainZero: function (num) {
var result = parseFloat(num);//把他转换成Float类型
if (isNaN(result)) {//判断result结果是否为nan
result = 0;//如果是把他变成0
}
result = Math.round(num * 10) / 10;//round函数:四舍五入 例如:5.867*10=58.67 ,58.7/10=5.87-->5.9
var s_x = result.toString(); //把result转换成tostring;
var pos_decimal = s_x.indexOf(‘.‘);//indexOf截取‘.‘后面的数
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += ‘.‘;
}

while (s_x.length <= pos_decimal + 1) {
s_x += ‘0‘;
}
return s_x;
},


//add by litan 20200420
//四舍五入保留2位小数(不够位数,则用0替补)
keepTwoContainZero: function (num) {
var result = parseFloat(num);
if (isNaN(result)) {
result = 0;
}
result = Math.round(num * 100) / 100;
var s_x = result.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;
},
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!