hoist-react icon indicating copy to clipboard operation
hoist-react copied to clipboard

Issues with `fmtPercent` + `zeroPad` due to JS FP trailing precision

Open jskupsik opened this issue 6 months ago • 1 comments

The numeric ZeroPad does not work in some cases, due to javascript number weirdness.

Example:

fmtPercent(10.1, { precision: 4, zeroPad: 2 } )

Expected result: 10.10% Actual result: 10.1000%

Most values seem to work (ie, 10.2) but others do not (ie, 10.3).

The reason for this is the fact that fmtPercent multiplies by 100, and 0.101 * 100 -> 10.100000000000001 Therefore, the FormatNumber.countDecimalPlaces() function counts all of those 0s as required precision, and the zeroPad is ignored.

jskupsik avatar Aug 07 '24 21:08 jskupsik