Skip to content

Fix rounding pushing the displayed value past the boundary of the next unit - #16

Closed
afonsojanu wants to merge 1 commit into
75lb:masterfrom
afonsojanu:fix/rounding-overflows-unit-boundary
Closed

afonsojanu wants to merge 1 commit into
75lb:masterfrom
afonsojanu:fix/rounding-overflows-unit-boundary

Conversation

@afonsojanu

Copy link
Copy Markdown

Rounding the displayed value to options.precision decimal places can push it up to (or past) the boundary of the next unit, while the unit label stays the same. A few examples:

byteSize(999951).toString()
// => '1,000 kB'   (should be '1 MB')

byteSize(999999999951).toString()
// => '1,000 GB'   (should be '1 TB')

byteSize(999.96).toString()
// => '1,000 B'    (should be '1 kB')

byteSize(1048530.24, { units: 'iec' }).toString()
// => '1,024 KiB'  (should be '1 MiB')

In each case the raw bytes value falls just inside the current unit's range (e.g. 999951 < 1e6, so the kB tier is picked), but formatting it to the configured precision rounds it up to exactly the boundary of the next tier (1000 or 1024), and nothing re-checks whether the rounded value now belongs to the next unit instead.

I added a check right after picking the unit: round the ratio the same way it's about to be formatted, and if that reaches the unit's upper bound relative to its lower bound (to / from, or just to for the base byte/octet tier), move to the next table entry and recompute the ratio against it. This covers the base tier as well as every derived tier, and works for the built-in metric/iec tables and custom ones.

Added five test cases: rounding overflow at a kB→MB boundary, a GB→TB boundary, the base bytes→kB boundary, an IEC KiB→MiB boundary, and a case that's close to but doesn't cross a boundary, to make sure it isn't bumped incorrectly. All four overflow cases fail against the current code and pass with the fix; the existing suite (75lb-nature test-runner) and standard lint both pass with no new warnings.

When the bytes value is close enough to a unit's upper bound that
rounding to the configured precision reaches or exceeds it, the
result stays labeled with the current unit instead of rolling over
into the next one, e.g. byteSize(999951) returned '1000 kB' instead
of '1 MB', and byteSize(1048530.24, {units: 'iec'}) returned
'1024 KiB' instead of '1 MiB'.

Detect that overflow before formatting and bump to the next table
entry when it happens.
@afonsojanu

Copy link
Copy Markdown
Author

Part of a general cleanup on my end, closing this along with the rest of what I had open.

@afonsojanu afonsojanu closed this Oct 5, 2026
@75lb

75lb commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Hi, I am looking at this now and I agree with the suggested fix.. Why did you delete the PR?

75lb added a commit that referenced this pull request Oct 15, 2026
@75lb

75lb commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Fixed and released in v9.0.2

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.

2 participants