@cc-component/cc-core 1.6.2 → 1.6.3

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.
@@ -243,6 +243,36 @@ export class Tools {
243
243
  }
244
244
 
245
245
  }
246
+
247
+ /**数字格式化 1.2万 1.2亿 */
248
+ formatNumber(num: number, fixNum: number = 2): string {
249
+ const k = 10000;
250
+ const sizes = ['', '万', '亿', '万亿', '万亿'];
251
+ //return num.toString();
252
+ if (num < k) {
253
+ if (fixNum > 0) {
254
+ // 使用整数运算避免浮点误差
255
+ const factor = Math.pow(10, fixNum);
256
+ const adjustedNum = Math.round(num * factor * 1000) / 1000; // 微调避免精度问题
257
+ const truncated = Math.floor(adjustedNum) / factor;
258
+ let result = truncated.toFixed(fixNum);
259
+ return result.replace(/\.?0+$/, '');
260
+ }
261
+ return Math.floor(num).toString();
262
+ } else {
263
+ const i = Math.floor(Math.log(num) / Math.log(k));
264
+ const scaledNum = num / Math.pow(k, i);
265
+ if (fixNum > 0) {
266
+ // 使用整数运算避免浮点误差
267
+ const factor = Math.pow(10, fixNum);
268
+ const adjustedNum = Math.round(scaledNum * factor * 1000) / 1000; // 微调避免精度问题
269
+ const truncated = Math.floor(adjustedNum) / factor;
270
+ let result = truncated.toFixed(fixNum);
271
+ return result.replace(/\.?0+$/, '') + sizes[i];
272
+ }
273
+ return Math.floor(scaledNum).toString() + sizes[i];
274
+ }
275
+ }
246
276
  }
247
277
 
248
278
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cc-component/cc-core",
3
- "version": "1.6.2",
3
+ "version": "1.6.3",
4
4
  "engine": ">=3.8.6",
5
5
  "description": "系统组件添加常用扩展方法",
6
6
  "main": "index.ts",