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

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;

},

原文:https://www.cnblogs.com/ypxuedm/p/13396566.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值