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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @Description: 千分位转换
* @author cr
* @param {Number} value 金额
* @param {Number} decimal 要保留的小数位,默认两位
* @date 2020/4/18
* @time 12:05
*/
export function formatMoney(value: any, decimal: any) {
const _decimal = decimal || 2;
let number = toDecimal(value, _decimal);
number = executeLcommafy(number);
if (value === '' || (value == null && value !== 0)) {
return '';
}
return number;
}
/**
* 将金额转为万元(一万)(小数)
* @param amount String 金额
*/
export function toMillion(amount: any) {
amount = amount || 0;
amount = (parseFloat(amount) / 10000).toFixed(2);
var chararr = []; //用来存储金额字符串中的每一个字符
var rlength = amount.indexOf(".") > 0 ? amount.indexOf(".") : amount.length; //如果有小数点,则只考虑小数点之前的数字进行处理
for (let i = 0; i < amount.length; i++) {
if ((rlength - i > 0) && ((rlength - i) % 3 == 0) && (i != 0)) {
//长度减索引表示实际要处理字符串倒序的序号,如果能整除3则在前加逗号
chararr.push(',');
}
chararr.push(amount.charAt(i));
}
return chararr.join(''); //join方法重新将字符串数组拼接成字符串
}
/**
* @Description: 加千分位
* @author cr
* @date 2020/4/18
* @time 12:12
*/
function executeLcommafy(val: any) {
if (!val || !isNumber(val)) {
return '';
}
if (isNumber(val)) {
val = val.toString();
}
if (/^.*\..*$/.test(val)) {
const pointIndex = val.lastIndexOf('.');
let intPart = val.substring(0, pointIndex);
const pointPart = val.substring(pointIndex + 1, val.length);
intPart = intPart + '';
let re = /(-?\d+)(\d{3})/;
while (re.test(intPart)) {
intPart = intPart.replace(re, '$1,$2');
}
val = intPart + '.' + pointPart;
} else {
var re = /(-?\d+)(\d{3})/;
while (re.test(val)) {
val = val.replace(re, '$1,$2');
}
}
return val;
}
/**
* @Description: 保留小数位数
* @author cr
* @date 2020/4/18
* @time 12:01
*/
function toDecimal(value: any, decimal: any) {
if (isNumber(value)) {
if (isNumber(decimal)) {
return roundFixed(value, decimal);
} else {
return value;
}
} else {
return null;
}
}
/**
* @Description: 检测是否为数字
* @author cr
* @date 2020/4/18
* @time 12:01
*/
function isNumber(val: any) {
let regPos = /^\d+(\.\d+)?$/; //非负浮点数
let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
return regPos.test(val) || regNeg.test(val);
}
/**
* @Description: 四舍五入
* @author cr
* @date 2020/4/18
* @time 12:02
*/
function roundFixed(num: any, fixed: any) {
let pos = num.toString().indexOf('.');
let decimal_places = num.toString().length - pos - 1;
let _int = num * Math.pow(10, decimal_places);
let divisor_1 = Math.pow(10, decimal_places - fixed);
let divisor_2 = Math.pow(10, fixed);
const value = Math.round(_int / divisor_1) / divisor_2;
return prefixZero(value, fixed);
}
/**
* @Description: 精度
* @author cr
* @date 2020/4/18
* @time 12:03
*/
function prefixZero(value: any, num: any) {
let a, b, c, i;
a = value.toString();
b = a.indexOf('.');
c = a.length;
if (num === 0) {
if (b !== -1) {
a = a.substring(0, b);
}
} else {
/*如果没有小数点*/
if (b === -1) {
a = a + '.';
for (i = 1; i <= num; i++) {
a = a + '0';
}
} else {
/*有小数点,超出位数自动截取,否则补0*/
a = a.substring(0, b + num + 1);
for (i = c; i <= b + num; i++) {
a = a + '0';
}
}
}
return a;
}