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.1
0.1
0.1000
0.1000
0.1
0.1 + 0.2
0.30000000000000004
0.3000
0.3000
0.3
12.2 / 0.1
121.99999999999999
122.0000
122.0
122
-0.0000001
-1e-7
-0.0000
-1.000e-7
0
19.9 * 100
1989.9999999999998
1990.0000
1990
1990
1.9999999999
1.9999999999
2.0000
2.000
2
12345.60000002
12345.60000002
12345.6000
1.235e+4
12345.6
9999999.123000001
9999999.123000002
9999999.1230
1.000e+7
9999999.123
123.123456789
123.123456789
123.1235
123.1
123.123456789
0.30000000000000004
0.300
0.30000
0.3
Let'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]