|
|
@@ -1,17 +1,37 @@
|
|
|
+/* eslint-disable no-param-reassign */
|
|
|
/* eslint-disable no-restricted-globals */
|
|
|
// 判断传入的是否是数字或者可转为数字的字符串
|
|
|
export const isNumber = (value: string | number): boolean => {
|
|
|
return /^(-|\+)?\d+(\.\d+)?$/.test(value as string);
|
|
|
};
|
|
|
|
|
|
+export const innerRound = (
|
|
|
+ num: number,
|
|
|
+ lenght: number,
|
|
|
+ decimal: number
|
|
|
+): number => {
|
|
|
+ let value;
|
|
|
+ let pre = 1;
|
|
|
+ if (lenght === 0) return num;
|
|
|
+ if (num < 0) pre = -1; // 负数的时候先变成正数
|
|
|
+ num *= pre;
|
|
|
+ const n = 10 ** lenght;
|
|
|
+ value = Math.round(num * n);
|
|
|
+ if (lenght <= decimal) {
|
|
|
+ return (value / n) * pre;
|
|
|
+ }
|
|
|
+ value = Math.round(value / 10 ** (lenght - decimal));
|
|
|
+ return (value / 10 ** decimal) * pre;
|
|
|
+};
|
|
|
+
|
|
|
export const roundForObj = (obj: string | number, decimal: number): number => {
|
|
|
let value;
|
|
|
if (obj === undefined || obj === null || isNaN(obj as number)) return 0;
|
|
|
- const n = 10 ** decimal;
|
|
|
+ const lenght = 10;
|
|
|
if (obj === +obj) {
|
|
|
- value = Math.round(obj * n) / n;
|
|
|
+ value = innerRound(obj, lenght, decimal);
|
|
|
} else {
|
|
|
- value = Math.round(Number(obj) * n) / n;
|
|
|
+ value = innerRound(Number(obj), lenght, decimal);
|
|
|
}
|
|
|
return value;
|
|
|
};
|