angular -filter过滤器金额处理加小数点后两位

雷硕
2023-12-01
function toDecimal2(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
                return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
                rs = s.length;
                s += '.';
        }
        while (s.length <= rs + 2) {
                s += '0';
        }
        return s;
}

angular-filter:

// 金额小数点
app.filter('moneyFixed',function() { //可以注入依赖
    return function(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    }
});

调用:

 <td style="color:#0033FF;">{{(item.accountBalance/100)|moneyFixed}}元</td>

 

 

 类似资料: