| 7 | * 支持:正负整数、小数、字符串格式的数值 |
| 8 | */ |
| 9 | export function formatNumber(value: any): string | number { |
| 10 | if (value === null || value === undefined || value === '') { |
| 11 | return value |
| 12 | } |
| 13 | |
| 14 | let str: string |
| 15 | if (typeof value === 'string') { |
| 16 | str = value.trim() |
| 17 | } else if (typeof value === 'number') { |
| 18 | str = String(value) |
| 19 | } else { |
| 20 | return value |
| 21 | } |
| 22 | |
| 23 | const match = str.match(/^([+-])?(\d+)(\.(\d+))?$/) |
| 24 | if (!match) { |
| 25 | return value |
| 26 | } |
| 27 | |
| 28 | const sign = match[1] || '' |
| 29 | const intPart = match[2] |
| 30 | const decPart = match[3] || '' |
| 31 | |
| 32 | const formattedInt = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',') |
| 33 | |
| 34 | return sign + formattedInt + decPart |
| 35 | } |
| 36 | |
| 37 | interface CheckedData { |
| 38 | isPercent: boolean |