-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomdify.js
More file actions
34 lines (31 loc) · 939 Bytes
/
comdify.js
File metadata and controls
34 lines (31 loc) · 939 Bytes
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
/**
* 给数字加上千分位
*/
exports.comdify = function (n) {
var re = /\d{1,3}(?=(\d{3})+$)/g;
var n1 = n.replace(/^(\d+)((\.\d*)?)$/, function (s, s1, s2) { return s1.replace(re, "$&,") + s2; });
return n1;
}
exports.toThousands = function (num) {
var result = '', counter = 0;
num = (num || 0).toString();
var nums = num.split(".");
var moy = 0;
if (nums.length > 1) {
moy = nums[0];
for (var i = moy.length - 1; i >= 0; i--) {
counter++;
result = moy.charAt(i) + result;
if (!(counter % 3) && i != 0) { result = ',' + result; }
}
result = result + "." + nums[1];
} else {
moy = nums[0];
for (var i = moy.length - 1; i >= 0; i--) {
counter++;
result = moy.charAt(i) + result;
if (!(counter % 3) && i != 0) { result = ',' + result; }
}
}
return result;
}