元、万元、亿元、万亿元转换(递归、闭包)

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
filterMoney(value) {
var count = 0;
var baseList = ["元", "万元", "亿元", "万亿元"];
function filter(value) {
let moneyLen = String(Math.abs(Number(value))).split(".")[0].length;
let money = Number(value);

if (moneyLen < 5) {
return { num: money, base: 1, unit: baseList[count] };
} else {
count++;
var num = (money / 10000).toFixed(2);
if (num >= 10000) {
return filter(num);
} else {
return {
num: num,
base: Math.pow(10, 4 * count),
unit: baseList[count]
};
}
}
}
return filter(value);
}