-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.千分位.html
44 lines (37 loc) · 1.15 KB
/
10.千分位.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 千分位
const formatNum = (num, n = 1) => {
var f = parseFloat(num);
if (isNaN(f)) {
return false;
}
f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
var s = f.toString();
var rs = s.indexOf('.');
//判定如果是整数,增加小数点再补0
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + n) {
s += '0';
}
const regex = new RegExp(`(?!^)(?=(\\d{3})+${s.includes('.') ? '\\.' : '$'})`, 'g')
return s.replace(regex, ',')
}
console.log(formatNum(1314520.678));
// 创建临时id
const randomString = () => Math.random().toString(36).slice(2);
console.log(randomString());
</script>
</head>
<body>
</body>
</html>