correctFloat
Corrects floating-point arithmetic errors by rounding to a given number
of significant digits. Useful after calculations that accumulate binary
floating-point drift (e.g. 0.1 + 0.2 === 0.30000000000000004).
The default precision of 14 significant digits eliminates typical
rounding noise for values in the range used by most applications.
Note: for values whose integer part already consumes 14 or more digits
(i.e. |value| ≥ 1e13), toPrecision(14) has no room left for decimal
digits and will silently truncate them. Increase precision if you
need to correct drift in very large numbers.
Note: IEEE-754 doubles carry at most ~17 significant decimal digits. Precision values above 17 pad with digits that reflect the underlying binary representation rather than correcting drift.
Available since v2.0.2
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | number | The floating-point value to correct |
precision | number | Integer number of significant digits between 1 and 100 (default: 14). Values above 17 are valid but expose binary noise beyond IEEE-754’s meaningful range. |
Returns
Section titled “Returns”number — The corrected value
Examples
Section titled “Examples”Fix floating-point drift
Section titled “Fix floating-point drift”Corrects the classic 0.1 + 0.2 floating-point arithmetic error.
Custom significant-digit precision
Section titled “Custom significant-digit precision”Pass a second argument to control how many significant digits to keep.
