number → string,
but looks good
Automatically detects and fixes floating-point precision issues • [GitHub]
Floating-point arithmetic creates ugly precision artifacts that need smart formatting.
Native APIs require fixed parameters. For example `toFixed(4)` works for 123.45670001 but destroys 0.0000001 → “0.0000”.
Tricks like `+(n.toFixed(4)).toString()` remove extra 0s but still requires a fixed decimal length and it is still rounding (changing) the value.
`nstr()` automatically fixes potential number errors, and formats numbers to string without losing precisions.
0.10.10.10000.10000.10.1 + 0.20.300000000000000040.30000.30000.312.2 / 0.1121.99999999999999122.0000122.0122-0.0000001-1e-7-0.0000-1.000e-7019.9 * 1001989.99999999999981990.0000199019901.99999999991.99999999992.00002.000212345.6000000212345.6000000212345.60001.235e+412345.69999999.1230000019999999.1230000029999999.12301.000e+79999999.123123.123456789123.123456789123.1235123.1123.1234567890.300000000000000040.3000.300000.3Let's trace through the algorithm using `0.14499999582767487` as an example:
maxDecimals (default: 10) - Maximum decimal places to considerthreshold (default: 4) - Minimum consecutive 0s/9s to trigger cleanupBuilt by [@shuding_] with [v0.app]