Skip to content

fix: bump the unit when precision reaches the ceiling, without rounding first - #414

Open
bardrop wants to merge 2 commits into
avoidwork:masterfrom
bardrop-oss:fix-precision-unit-ceiling
Open

bardrop wants to merge 2 commits into
avoidwork:masterfrom
bardrop-oss:fix-precision-unit-ceiling

Conversation

@bardrop

@bardrop bardrop commented Nov 15, 2026 •

Copy link
Copy Markdown

Summary

applyPrecisionHandling() now also moves to the next unit when toPrecision() reaches the unit ceiling, not only on e-notation.

Since #404, (999.999).toPrecision(4) is "1000", which has no e, so filesize(999999, { precision: 4 }) printed 1000 kB (and 1048575 with base: 2 printed 1024 KiB). It now prints 1.000 MB / 1.000 MiB, as 11.0.30 did and as filesize(999999) → 1 MB does.

The helper signature, roundingFunc, round and the rounding step are unchanged (an earlier version of this PR removed them; that is reverted after review).

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature / enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Build / dependency / tooling change
  • Test additions or updates
  • Other

Related Issues

Fixes #413

Testing

  • One new test in Precision option (ceiling bump for SI / IEC / bits / negative / object and exponent output / forced exponent). It fails on master (1000 kB) and passes with the fix.
  • npm test: lint clean, 280 pass / 0 fail, coverage still 100% for all files.

Checklist

  • npm test passes
  • npm run build succeeds
  • 100% test coverage maintained
  • No hardcoded secrets or credentials introduced
  • Zero external dependencies added
  • ES Modules only (no CommonJS in src/)
  • JSDoc comments added/updated
  • CHANGELOG.md updated (if applicable)

…ng first

applyPrecisionHandling() only moved to the next unit when toPrecision()
returned e-notation. Since avoidwork#404 the value is no longer rounded to `round`
before the precision pass, so 999.999 kB with precision 4 gives "1000"
and printed "1000 kB" (filesize(999999) prints "1 MB"). Move up when the
significant-digit form reaches `ceil` too.

When the unit moved, the recomputed value was still rounded to `round`
decimals before toPrecision(), the same double rounding avoidwork#399 fixed in the
main path: 1000 KiB printed "0.980 MiB" instead of "0.977 MiB", and with
round: 0, 392 B at precision 1 printed "0 kB". Take the new value straight
from the input; roundingFunc and round are no longer needed here.

Fixes avoidwork#413
@avoidwork

Copy link
Copy Markdown
Owner

I think i'm going to roll back the recent changes as they are incomplete.

Only add the unit ceiling check; restore the rounding step and its
parameters, as requested in review.
@bardrop

bardrop commented Nov 16, 2026

Copy link
Copy Markdown
Author

Fair point, sorry about that. I pushed 3c8efc3, which puts roundingFunc and round back, together with the rounding step, so the helper signature and the round/roundingMethod behavior are the same as on master. The PR now only adds the unit ceiling check, so filesize(999999, { precision: 4 }) gives 1.000 MB again instead of 1000 kB. The 0.990 GiB tests are back as they were. 280 tests pass with 100% coverage.

If you roll back #404 instead, that also fixes the 1000 kB case, and I am happy to close this PR then.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: precision leaves 1000 kB in the old unit and rounds to round before the unit bump

2 participants