设置金钱格式
/** * 设置金钱格式 * @param {Number} value [金钱数值] */ function Money(value, symbol = "¥") { if (value) { var pre = decimal = s1 = s2 = ''; value = String(value) s1 = (value.split('.')[0]).split('').reverse().join(''); decimal = value.split('.')[1] || '' if (isNaN(Number(s1[s1.length - 1]))) { pre = s1[s1.length - 1]; s1 = s1.substring(0, s1.length - 1); } for (var i = 0, n = s1.length; i < n; i++) { (i % 3 == 0 && i > 0 && s1) ? s2 += ',' + (s1[i] || ''): s2 += (s1[i] || '') } return symbol + pre + s2.split('').reverse().join('') + (decimal ? '.' + decimal : ''); } else if(value === 0){ return symbol + value } }
结果如下: