• 格式化为金额格式


            function formatPayment(num) {
                var unit = "";
                if (num < 10000) {
                    return formatCurrency(num, 2);
                }
                else if (num < 100000000) {
                    num = num / 10000;
                    unit = "万";
                }
                else {
                    num = num / 100000000;
                    unit = "亿";
                }
                return formatCurrency(num, 4) + unit;
            }
    
            //将数值四舍五入(保留2位小数)后格式化成金额形式
            function formatCurrency(num, n) {
                num = num.toString().replace(/\$|\,/g, '');
                if (isNaN(num))
                    num = "0";
                sign = (num == (num = Math.abs(num)));
                var m = Math.pow(10, n);
                num = Math.floor(num * m + 0.50000000001);
                cents = num % m;
                num = Math.floor(num / m).toString();
                while (cents.toString().length < n)
                    cents = "0" + cents;
                for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
                    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
                return (((sign) ? '' : '-') + num + '.' + cents);
            }

    如果对您有帮助,请捐助我

  • 相关阅读:
    我们是如何实现DevOps的
    cmp命令
    全排列II
    chown命令
    两个数组的交集II
    MVVM模式的理解
    chmod命令
    路径总和
    cat命令
    跳水板
  • 原文地址:https://www.cnblogs.com/wangjiayuan/p/4088706.html
Copyright © 2020-2023  润新知