金钱格式化

开发过程中经常能遇到对金钱格式化的需求,这里整理一个方法

es5 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 将数值四舍五入后格式化成金额形式
*
* @param num 数值(Number或者String)
* @param options.prefix 金钱前缀,默认为空,一般为 ¥ 或 $
* @param options.decimalNum 保留小数点个数,默认为2 一般为 0 1 2
* @param options.splitSymbol 格式化分割符,默认为英文逗号,分隔符必须是单字符
* @return 金额格式的字符串,如'¥1,234,567.45'
* @type String
*/
function formatCurrency(num, options) {
if (options === undefined) options = {};
var prefix = options.prefix || '';
var decimalNum = options.decimalNum;
var splitSymbol = options.splitSymbol;
var centsPercent = 100;
if (splitSymbol === undefined) splitSymbol = ',';
if (decimalNum !== 0 && decimalNum !== 1 && decimalNum !== 2) decimalNum = 2;
if (decimalNum === 0) {
centsPercent = 1;
}
if (decimalNum === 1) {
centsPercent = 10;
}
num = num.toString().replace(/\$|\,/g, '');
if (isNaN(num)) num = "0";
var sign = (num == (num = Math.abs(num))) ? '' : '-';
num = Math.floor(num * centsPercent + 0.50000000001);
var cents = num % centsPercent;
num = Math.floor(num / centsPercent).toString();
if (cents < 10 && decimalNum === 2) {
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)) + splitSymbol + num.substring(num.length - (4 * i + 3));
}
if (decimalNum === 0) {
return prefix + sign + num;
}
return prefix + sign + num + '.' + cents;
}

console.log(formatCurrency(9090900.55533));
// output: 9,090,900.56

console.log(formatCurrency(9090900.55533, {
prefix: '¥',
decimalNum: 2,
splitSymbol: ','
}));
// output: ¥9,090,900.56

es6 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

/**
* 将数值四舍五入后格式化成金额形式
*
* @param num 数值(Number或者String)
* @param options 可选参数
* @param options.prefix 金钱前缀,默认为空,一般为 ¥ 或 $
* @param options.decimalNum 保留小数点个数,默认为2 一般为 0 1 2
* @param options.splitSymbol 格式化分割符,默认为英文逗号,分隔符必须是单字符
* @return 金额格式的字符串,如'¥1,234,567.45'
* @type String
*/
function formatCurrency(num, options = {}) {
let {decimalNum, splitSymbol} = options;
const {prefix = ''} = options;
let centsPercent = 100;

if (splitSymbol === undefined) splitSymbol = ',';
if (decimalNum !== 0 && decimalNum !== 1 && decimalNum !== 2) decimalNum = 2;
if (decimalNum === 0) centsPercent = 1;
if (decimalNum === 1) centsPercent = 10

num = num.toString().replace(/\$|,/g, '');
if (isNaN(num)) num = '0';
const sign = (num === Math.abs(num).toString()) ? '' : '-';
num = Math.abs(num);
num = Math.floor((num * centsPercent) + 0.50000000001);
let cents = num % centsPercent;
num = Math.floor(num / centsPercent).toString();
if (cents < 10 && decimalNum === 2) {
cents = `0${cents}`;
}
for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
const endPosition = (4 * i) + 3;
num = num.substring(0, num.length - endPosition)
+ splitSymbol + num.substring(num.length - endPosition);
}
if (decimalNum === 0) {
return prefix + sign + num;
}
return `${prefix}${sign}${num}.${cents}`;
}