Repository navigation
Fix rounding pushing the displayed value past the boundary of the next unit - #16
Closed
afonsojanu wants to merge 1 commit into
Closed
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
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.
Author
|
Part of a general cleanup on my end, closing this along with the rest of what I had open. |
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
Owner
|
Fixed and released in v9.0.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rounding the displayed value to
options.precisiondecimal places can push it up to (or past) the boundary of the next unit, while the unit label stays the same. A few examples: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 (1000or1024), 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 justtofor 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) andstandardlint both pass with no new warnings.