# 添加千分符
const str = "100000000000",
reg = /(?=(\B\d{3})+$)/g;
// reg = /(?=(\B\d{3})+($|\.))/g;
str.replace(reg, ",");
1
2
3
4
2
3
4
但是会与一个问题:
var a = '123456789.9656789';
a.replace(/(?=(\B\d{3})+(\.|$))/g, ',');// '123,456,789.9,656,789'
1
2
2
总解决方案:
var a = '123456789.9656789';
a.replaceAll(/(?<!\.\d*)(?=(\B\d{3})+($|\.))/g, ',');// '123,456,789.9656789'
1
2
2
# 保留小数
function formatPrecision() {
let [int, decimal=''] = val.split('.');
const decimalLength = decimal.length;
if (decimalLength > precision) {
if (precision === 0) {
const next = decimal[0];
if (next > 4) {
int++;
}
return String(int);
}
let pre = decimal.slice(0, precision - 1);
let current = decimal[precision - 1];
const next = decimal[precision];
if (next > 4) {
current++;
if (current > 9) {
current = current % 10;
const oldPreLength = pre.length;
pre++;
pre = pre.toString();
if (pre.length > oldPreLength) {
pre = pre.slice(1);
}
int++;
}
}
decimal = '.' + pre + current;
} else if (decimalLength < precision) {
const delta = precision - decimalLength;
let i = 0;
while (i<delta) {
decimal += '0';
i++;
}
decimal = '.' + decimal;
}
return int + decimal;
}
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
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
← instanceof Loading →