-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path千分位分隔符.html
64 lines (52 loc) · 1.91 KB
/
千分位分隔符.html
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
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function f(str) {
const ret = Array.from(str).reverse().reduce((result, next, i, arr) => {
if ((i + 1) % 3 === 0 && (i + 1) !== arr.length) {
console.log(next);
result.push(next, ',')
return result;
}
result.push(next);
return result;
// return (index % 3) ? (next + "" + pre) : (next + ',' + pre);
}, [])
return ret.reverse().join('');
}
// console.log(f("12345678"));
let arr = [1, 2, 3];
/**
0. 如何将浮点数点左边的数每三位添加一个逗号, 如 12000000.11 转化为『 12, 000, 000.11』 ?
使用正则表达式方法
**/
// function format(number) {
// return number && number.replace(/(\d)(?=(\d{3}+\.))/g, function ($1, $2, $3) {
// return $2 + ',';
// })
// }
// console.log(format("12345678"));
// ?= pattern 表示匹配到pattern的开始位置的字符, 例如window( ? = 95 | 98 | 2000 | xp), 可以匹配到window2000中的window。
function fd(s) {
let ret = Array.from(s).reverse().reduce((result, item, i, arr) => {
if ((i + 1) % 3 === 0 && (i + 1) !== arr.length) {
result.push(item, ",");
return result;
}
result.push(item);
return result;
}, [])
return ret.reverse().join('');
}
console.log(fd("12345678"));
console.log(Array.from("12345678"));
console.log(Array.of(7));
</script>
</head>
<body>
</body>
</html>