当前位置: 移动技术网 > IT编程>网页制作>CSS > 金额格式化处理千分位金额逗号,隔开

金额格式化处理千分位金额逗号,隔开

2019年01月02日  | 移动技术网IT编程  | 我要评论

降哥环保系列,池贤秀,三年级上册语文教案

方法1.

//处理千分位使用
var dealthousands = function(value) {
    if (value === 0) {
        return parsefloat(value).tofixed(2);
    }
    if (value != "") {
        var num = "";
        value += "";//转化成字符串
        value = parsefloat(value.replace(/,/g, '')).tofixed(2);//若需要其他小数精度,可将2改成变量
        if (value.indexof(".") == -1) {
            num = value.replace(/\d{1,3}(=(\d{3})+$)/g, function(s) {
                return s + ',';
            });
        } else {
            num = value.replace(/(\d)(=(\d{3})+\.)/g, function(s) {
                return s + ',';
            });
        }
    } else {
        num = ""
    }
    return num;
}

方法2.

/*
 * formatmoney(s,type)
 * 功能:金额按千位逗号分割
 * 参数:s,需要格式化的金额数值.
 * 参数:type,(number类型)金额的小数位.
 * 返回:返回格式化后的数值.
 */
var formatmoney= (val,type) => {
    if(val === '' || val === 0) return '0.00'
    val = number(val)
    if(isnan(val)) return ''
    return val.tofixed(type).replace(/(\d)(=(\d{3})+\.)/g, '$1,')
  }

方法3.

formatmoney = function(s, type) {
    // if (/[^0-9\.]/.test(s))
    //     return "0.00";
    // if(isnan(val)) return ''
    if (s == null || s == "" || s == 0)
        return "0.00";
    s = number(s)
    var result = s;
    if(s<0){
        s=0 - s;
    }
    if(isnan(s)) return ''
    s = s.tostring().replace(/^(\d*)$/, "$1.");
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s))
        s = s.replace(re, "$1,$2");
    s = s.replace(/,(\d\d)$/, ".$1");
    if (type == 0) {// 不带小数位(默认是有小数位)
        var a = s.split(".");
        if (a[1] == "00") {
            s = a[0];
        }
    }
    if(result<0){
        s = '-'+s
    }
    return s;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网