feat: 数据看板
This commit is contained in:
56
src/utils/tools.ts
Normal file
56
src/utils/tools.ts
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* @Author: RenXiaoDong
|
||||
* @Date: 2025-06-27 17:36:31
|
||||
*/
|
||||
export function toFixed(num: number | string, n: number): number {
|
||||
return parseFloat(parseFloat(num.toString()).toFixed(n));
|
||||
}
|
||||
|
||||
export function isNotData(n: number): boolean {
|
||||
if (n === undefined) {
|
||||
return true;
|
||||
}
|
||||
return n === -2147483648;
|
||||
}
|
||||
|
||||
export function splitNumber(num: number): string | number {
|
||||
if (!num) {
|
||||
return num;
|
||||
}
|
||||
const parts = num.toString().split('.');
|
||||
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
return parts.join('.');
|
||||
}
|
||||
|
||||
export function formatNumberShow(...args: any[]): string | number {
|
||||
const [_args] = args;
|
||||
const { value, len = 2, split = true, showExactValue = false } = typeof _args === 'object' ? _args : { value: _args };
|
||||
const getNumber = (value: number) => {
|
||||
return split ? splitNumber(value) : value;
|
||||
};
|
||||
|
||||
if (isNotData(value)) {
|
||||
return '-';
|
||||
}
|
||||
if (value < 0) {
|
||||
return `-${formatNumberShow({
|
||||
value: -value,
|
||||
len,
|
||||
split,
|
||||
showExactValue,
|
||||
})}`;
|
||||
}
|
||||
if (showExactValue) {
|
||||
return getNumber(toFixed(value, len));
|
||||
}
|
||||
if (value < 10000) {
|
||||
return getNumber(toFixed(value, len));
|
||||
} else if (value < 100000000) {
|
||||
const _n = Math.round((value / 10000) * 100) / 100;
|
||||
return split ? `${splitNumber(_n)}w` : `${toFixed(_n, len)}w`;
|
||||
} else {
|
||||
const _n = Math.round((value / 100000000) * 100) / 100;
|
||||
|
||||
return split ? `${splitNumber(_n)}亿` : `${toFixed(_n, len)}亿`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user