oak-domain 5.1.31 → 5.1.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,5 +2,5 @@ declare const ToCent: (float: number) => number;
2
2
  declare const ToYuan: (int: number) => number;
3
3
  declare const StringToCent: (value: string, allowNegative?: true) => number | undefined;
4
4
  declare const CentToString: (value: number, fixed?: number) => string | undefined;
5
- declare const ThousandCont: (value: number | string, decimalPlaces?: number) => string | undefined;
5
+ declare const ThousandCont: (value: number | string, decimalPlaces?: number) => string;
6
6
  export { ToCent, ToYuan, StringToCent, CentToString, ThousandCont };
@@ -23,29 +23,40 @@ const CentToString = (value, fixed) => {
23
23
  };
24
24
  exports.CentToString = CentToString;
25
25
  const ThousandCont = (value, decimalPlaces) => {
26
- let value1 = typeof value === 'number' ? `${value}` : value;
27
- const numArr = value1.split('.');
28
- value1 = numArr[0];
29
- let result = '';
30
- while (value1.length > 3) {
31
- result = ',' + value1.slice(-3) + result;
32
- value1 = value1.slice(0, value1.length - 3);
33
- }
34
- if (value1) {
35
- result = value1 + result;
36
- }
37
- if (decimalPlaces && decimalPlaces > 0) {
38
- if (numArr[1]) {
39
- const decimalPart = numArr[1].padEnd(decimalPlaces, '0').slice(0, decimalPlaces);
40
- result = result + '.' + decimalPart;
41
- }
42
- else {
43
- result = result + '.' + '0'.repeat(decimalPlaces);
44
- }
26
+ let valueStr = typeof value === 'number' ? value.toString() : value;
27
+ // 处理负号
28
+ let isNegative = false;
29
+ if (valueStr.startsWith('-')) {
30
+ isNegative = true;
31
+ valueStr = valueStr.slice(1); // 移除负号
32
+ }
33
+ // 分离整数部分和小数部分
34
+ const [integerPart, decimalPart = ''] = valueStr.split('.');
35
+ // 千分位格式化整数部分
36
+ let formattedInteger = '';
37
+ let remainingInteger = integerPart;
38
+ while (remainingInteger.length > 3) {
39
+ formattedInteger = ',' + remainingInteger.slice(-3) + formattedInteger;
40
+ remainingInteger = remainingInteger.slice(0, remainingInteger.length - 3);
41
+ }
42
+ if (remainingInteger) {
43
+ formattedInteger = remainingInteger + formattedInteger;
44
+ }
45
+ // 恢复负号
46
+ if (isNegative) {
47
+ formattedInteger = '-' + formattedInteger;
48
+ }
49
+ // 处理小数部分
50
+ let formattedDecimal = '';
51
+ if (decimalPlaces !== undefined && decimalPlaces > 0) {
52
+ formattedDecimal = decimalPart.padEnd(decimalPlaces, '0').slice(0, decimalPlaces);
53
+ return formattedInteger + '.' + formattedDecimal;
54
+ }
55
+ else if (decimalPart) {
56
+ return formattedInteger + '.' + decimalPart;
45
57
  }
46
58
  else {
47
- result = numArr[1] ? result + '.' + numArr[1] : result;
59
+ return formattedInteger;
48
60
  }
49
- return result;
50
61
  };
51
62
  exports.ThousandCont = ThousandCont;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-domain",
3
- "version": "5.1.31",
3
+ "version": "5.1.32",
4
4
  "author": {
5
5
  "name": "XuChang"
6
6
  },