| 1 | import type { BigDecimal } from '../../big-decimal/index.js'; |
| 2 | import type { DisplayDigits } from '../types-kernel-serialization.js'; |
| 3 | |
| 4 | /** |
| 5 | * Round a value to `n` significant figures, returning a value of the same kind |
| 6 | * (used by the number-display `{ significant: n }` control). |
| 7 | * |
| 8 | * Truncation only — no trailing-zero padding. For machine numbers, the value |
| 9 | * is round-tripped through `parseFloat(x.toPrecision(n))` so that the caller |
| 10 | * can serialize it through the normal string path; this avoids the exponential |
| 11 | * notation that `Number.prototype.toPrecision` injects for large/small |
| 12 | * magnitudes (e.g. `(1500).toPrecision(2)` → `"1.5e+3"` would otherwise leak |
| 13 | * into fixed-notation output). |
| 14 | */ |
| 15 | export function roundToSignificant(value: number, n: number): number; |
| 16 | export function roundToSignificant(value: BigDecimal, n: number): BigDecimal; |
| 17 | export function roundToSignificant( |
| 18 | value: number | BigDecimal, |
| 19 | n: number |
| 20 | ): number | BigDecimal; |
| 21 | export function roundToSignificant( |
| 22 | value: number | BigDecimal, |
| 23 | n: number |
| 24 | ): number | BigDecimal { |
| 25 | if (typeof value === 'number') { |
| 26 | if (!Number.isFinite(value)) return value; |
| 27 | return parseFloat(value.toPrecision(n)); |
| 28 | } |
| 29 | return value.toPrecision(n); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Round a value to `k` digits after the decimal point (`toFixed` semantics), |