(value: number, precision = 0)
| 139 | * @param demarcationLen Several digits show the demarcation point for scientific notation |
| 140 | */ |
| 141 | export function numberToShow(value: number, precision = 0): string | null { |
| 142 | value = Number(value); |
| 143 | if (isNaN(value)) { |
| 144 | return 'NaN'; |
| 145 | } |
| 146 | |
| 147 | if (value === Infinity) { |
| 148 | return 'Infinity'; |
| 149 | } |
| 150 | |
| 151 | let str = value.toString(); |
| 152 | |
| 153 | const integerCount = str.split('.')[0]!.length; |
| 154 | const demarcationLen = 17; // 17 digits represent the demarcation point for scientific notation |
| 155 | // When the integer number is greater than 17, it needs to be displayed in scientific notation form |
| 156 | if (integerCount >= demarcationLen || (str.includes('e') && !str.includes('e-'))) { |
| 157 | const significanceDigitCount = 5; // number of significant digits after the decimal point |
| 158 | // There will also be precision problems, but because there are already precision requirements in the premise, there will be no rounding problems |
| 159 | str = value.toExponential(significanceDigitCount); |
| 160 | } else { |
| 161 | str = toFixed(value, precision); |
| 162 | } |
| 163 | |
| 164 | return str; |
| 165 | } |
| 166 | const CapacityUnit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
| 167 | // decimal point by 1 |
| 168 | // For example, 1.23 and 1.26 both return 1.3 |
no test coverage detected