-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (52 loc) · 1.41 KB
/
index.js
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
var $start = document.querySelector('#start');
var $end = document.querySelector('#end');
var $countBefore = document.querySelector('#count_before');
var $count = document.querySelector('#count');
$start.addEventListener('input', function (e) {
var startValue = $start.value || 0
var endValue = $end.value || 0
var count = parseFloat(startValue * endValue).toFixed(2);
$countBefore.innerHTML = count
var formatCount = formatNumber(count)
$count.innerHTML = formatCount
})
$end.addEventListener('input', function (e) {
var startValue = $start.value || 0
var endValue = $end.value || 0
var count = parseFloat(startValue * endValue).toFixed(2);
$countBefore.innerHTML = count
var formatCount = formatNumber(count)
$count.innerHTML = formatCount
})
function formatNumber(num) {
var regex = /(^-?)(\d+)(\.\d{1,2})?$/
var matches = regex.exec(num)
// console.log(matches)
if (matches == null) {
return '数据格式错误';
}
// 整数部分
var matchesInt = matches[2];
var resultArr = []
if (matchesInt.length) {
var n = matchesInt.length - 1;
var c = 0;
while (n >= 0) {
if (c == 3) {
resultArr.unshift(',')
c = 0;
}
resultArr.unshift(matchesInt[n])
c++
n--
}
}
var format = resultArr.join('')
if (matches[1]) {
format = matches[1] + format
}
if (matches[3]) {
format = format + matches[3]
}
return format
}