Skip to content

fix: apply precision to the unrounded value to avoid double rounding - #416

Merged
avoidwork merged 3 commits into
masterfrom
test/precision-ethos
Nov 16, 2026
Merged

avoidwork merged 3 commits into
masterfrom
test/precision-ethos

Conversation

@avoidwork

@avoidwork avoidwork commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds unit tests for precision rounding at unit boundaries and with the round option, and fixes the bug they exposed: when precision produced scientific notation, applyPrecisionHandling rounded the value to round decimals and then applied toPrecision, rounding twice. It now applies toPrecision to the unrounded value (src/helpers.js). No public API or signature changes. dist/ is rebuilt.

Two existing tests expected 0.990 GiB/gibibytes for filesize(1058223158, {precision: 3, ...}). That was an artifact of the double rounding; the true value is 0.98555 GiB, so they now expect 0.986.

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

Refs #399, #404, #414, #415 (precision rounding)

Testing

npm test (lint + node:test with coverage): 283 tests, 283 pass, 0 fail. The 3 new precision rounding tests previously failed and now pass. Coverage is 100% statements/branches/functions/lines for filesize.js and helpers.js. npm run build succeeds.

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 (N/A: no new or changed signatures)
  • CHANGELOG.md updated (N/A: generated by auto-changelog)

Screenshots (if applicable)

N/A

Closes #413

Adds five unit tests covering precision rounding behavior:
- 999999 with precision 4 renders as 1.000 MB
- 1048575 with base 2 and precision 4 renders as 1.000 MiB
- 1024000 with base 2 and precision 3 renders as 0.977 MiB
- 1058223158 with base 2 and precision 3 renders as 0.986 GiB
- 392 with precision 1 and round 0 renders as 0.4 kB
@avoidwork avoidwork self-assigned this Nov 16, 2026
@avoidwork avoidwork changed the title test: add precision rounding unit tests fix: apply precision to the unrounded value to avoid double rounding Nov 16, 2026
@avoidwork
avoidwork merged commit b5e828b into master Nov 16, 2026
2 checks passed
@avoidwork
avoidwork deleted the test/precision-ethos branch November 16, 2026 13:08
@avoidwork avoidwork mentioned this pull request Nov 16, 2026
6 of 15 tasks
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

1 participant