(n: BigDecimal)
| 975 | * @category conversions |
| 976 | */ |
| 977 | export const format = (n: BigDecimal): string => { |
| 978 | const normalized = normalize(n) |
| 979 | if (Math.abs(normalized.scale) >= 16) { |
| 980 | return toExponential(normalized) |
| 981 | } |
| 982 | |
| 983 | const negative = normalized.value < bigint0 |
| 984 | const absolute = negative ? `${normalized.value}`.substring(1) : `${normalized.value}` |
| 985 | |
| 986 | let before: string |
| 987 | let after: string |
| 988 | |
| 989 | if (normalized.scale >= absolute.length) { |
| 990 | before = "0" |
| 991 | after = "0".repeat(normalized.scale - absolute.length) + absolute |
| 992 | } else { |
| 993 | const location = absolute.length - normalized.scale |
| 994 | if (location > absolute.length) { |
| 995 | const zeros = location - absolute.length |
| 996 | before = `${absolute}${"0".repeat(zeros)}` |
| 997 | after = "" |
| 998 | } else { |
| 999 | after = absolute.slice(location) |
| 1000 | before = absolute.slice(0, location) |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | const complete = after === "" ? before : `${before}.${after}` |
| 1005 | return negative ? `-${complete}` : complete |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Formats a given `BigDecimal` as a `string` in scientific notation. |
no test coverage detected
searching dependent graphs…