最近搜索

js提供了parseInt()和parseFloat()两个转换函数。

浏览:1112
管理员 2019-09-07 22:57

js 相加

浮点  小数点


前者把值转换成整数,int   

后者把值转换成浮点数。float



js  保留2位

t.toFixed(2);



在layui   table 中使用如下

{{#  if(d.xianjin_xiaoshou != null){ }}
	 {{ (d.xianjin_xiaoshou).toFixed(2) }}
  {{#  } else { }}
  {{#  } }}

  
上面不能用,用这个。
<script type="text/html" id="format_shiji">
{{#  if(d.shiji!= null){ }}
{{ parseFloat(d.shiji).toFixed(2) }}
{{#  } }}
</script>
<script type="text/html" id="format_yunxu">
{{#  if(d.yunxu!= null){ }}
{{ parseFloat(d.yunxu).toFixed(2) }}
{{#  } }}
</script>


{{#  if(d.yinlian_heji != null){ }}
	 {{ (d.xianjin_chuzhi+d.jizhang_chuzhi+d.yinlian_chuzhi+d.xianjin_cunyou+d.jizhang_cunyou+d.yinlian_cunyou).toFixed(2) }}
  {{#  } else { }}
  {{#  } }}


var b = parseInt("10.00")



我们最新项目2026年使用的是 Number 小程序和html页面都可以用

    // 2. 计算订单总价(使用你提供的公式,保留2位小数)
    // 1. 先转数字计算核心金额(不提前toFixed,避免字符串运算)
    const amount = Number(order.amount || 0);
    const chajia = Number(order.chajia || 0);
    const projectListTotalJine = Number(order.projectListTotalJine || 0);
    const youhui = Number(order.youhui || 0);
    const tuiJine = Number(order.tuiJine || 0); // 退款金额(数字类型)


    // 4. 尾款计算(纯数字运算)+ 保留2位小数
    const remainAmount = (totalAmountNum - paidAmountNum + tuiJine).toFixed(2);
    
    
    
    
    
    
// 格式化金额 
function formatMoney(num) {
    try {
        return Number(num).toFixed(2);
    } catch (e) {
        return '0.00';
    }
}


联系站长

站长微信:xiaomao0055

站长QQ:14496453