IDEA中HTML页面的数据的值,四舍五入保留一位小数或者保留两位小数
2021-01-15 07:12
标签:截取 else epo add length str enter fun contain IDEA中HTML页面的数据的值,四舍五入保留一位小数或者保留两位小数 标签:截取 else epo add length str enter fun contain 原文地址:https://www.cnblogs.com/ypxuedm/p/13396566.htmlhtml页面:
{
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 pos_decimal = s_x.length;
s_x += ‘.‘;
}
while (s_x.length 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 pos_decimal = s_x.length;
s_x += ‘.‘;
}
while (s_x.length s_x += ‘0‘;
}
return s_x;
},
下一篇:HTML代码-1
文章标题:IDEA中HTML页面的数据的值,四舍五入保留一位小数或者保留两位小数
文章链接:http://soscw.com/index.php/essay/42145.html