首页

格式化为金额格式

        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);
        }

 

posted @ 2014-11-11 09:30  HTTP.POST.COM  阅读(382)  评论(0编辑  收藏  举报
页脚