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

html页面: { field: ‘suppliersLevel‘, align: ‘center‘, title: ‘供应商级别‘, formatter:f

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;

},