@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.
- package/assets/core/home/Tools.ts +30 -0
- package/package.json +1 -1
|
@@ -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
|
|