|
|
@@ -7,31 +7,34 @@ export const isNumber = (value: string | number): boolean => {
|
|
|
|
|
|
export const innerRound = (
|
|
|
num: number,
|
|
|
- lenght: number,
|
|
|
+ length: number,
|
|
|
decimal: number
|
|
|
): number => {
|
|
|
let value;
|
|
|
let pre = 1;
|
|
|
- if (lenght === 0) return num;
|
|
|
+ if (length === 0) return num;
|
|
|
if (num < 0) pre = -1; // 负数的时候先变成正数
|
|
|
num *= pre;
|
|
|
- const n = 10 ** lenght;
|
|
|
+ const n = 10 ** length;
|
|
|
value = Math.round(num * n);
|
|
|
- if (lenght <= decimal) {
|
|
|
+ if (length <= decimal) {
|
|
|
return (value / n) * pre;
|
|
|
}
|
|
|
- value = Math.round(value / 10 ** (lenght - decimal));
|
|
|
+ value = Math.round(value / 10 ** (length - decimal));
|
|
|
return (value / 10 ** decimal) * pre;
|
|
|
};
|
|
|
|
|
|
-export const roundForObj = (obj: string | number, decimal: number): number => {
|
|
|
+export const roundForObj = (
|
|
|
+ obj: string | number | undefined | null,
|
|
|
+ decimal: number
|
|
|
+): number => {
|
|
|
let value;
|
|
|
if (obj === undefined || obj === null || isNaN(obj as number)) return 0;
|
|
|
- const lenght = 10;
|
|
|
+ const length = 10;
|
|
|
if (obj === +obj) {
|
|
|
- value = innerRound(obj, lenght, decimal);
|
|
|
+ value = innerRound(obj, length, decimal);
|
|
|
} else {
|
|
|
- value = innerRound(Number(obj), lenght, decimal);
|
|
|
+ value = innerRound(Number(obj), length, decimal);
|
|
|
}
|
|
|
return value;
|
|
|
};
|