| 12345678910111213141516171819202122232425 |
- /* eslint-disable no-restricted-globals */
- // 判断传入的是否是数字或者可转为数字的字符串
- export const isNumber = (value: string | number) => {
- return /^(-|\+)?\d+(\.\d+)?$/.test(value as string);
- };
- 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;
- if (obj === +obj) {
- value = Math.round(obj * n) / n;
- } else {
- value = Math.round(Number(obj) * n) / n;
- }
- return value;
- };
- export const roundToString = (
- obj: string | number,
- decimal: number
- ): string => {
- const value = roundForObj(obj, decimal);
- return value.toFixed(decimal);
- };
|