Skip to content

fix: move to the next unit when the displayed value rounds up - #213

Open
bardrop wants to merge 1 commit into
bytesize-rs:masterfrom
bardrop-oss:fix-display-rounding-carry
Open

bardrop wants to merge 1 commit into
bytesize-rs:masterfrom
bardrop-oss:fix-display-rounding-carry

Conversation

@bardrop

@bardrop bardrop commented Nov 20, 2026

Copy link
Copy Markdown

Fixes #212.

After the unit is picked, the value is rounded to the display precision. If that rounding reaches a whole unit (1023.999 KiB → 1024.0), the value is now shown in the next unit:

bytes format before after
1,048,575 iec 1024.0 KiB 1.0 MiB
999,999 si 1000.0 kB 1.0 MB
1,073,741,823 iec 1024.0 MiB 1.0 GiB
1,048,575 iec_short 1024.0K 1.0M
1,048,064 iec {:.0} 1024 KiB 1 MiB
1,048,575 iec {:.3} 1023.999 KiB 1023.999 KiB (no change)

The check is value >= unit - 0.5 * 10^-precision, so it follows the precision the caller asks for. It never goes past the largest prefix (u64::MAX is 16 EiB, so it can't reach that anyway).

The existing display_matches_just_below_pebi_boundary test expected "1024.0 TiB" for 1024^5 - 1 bytes; it now expects "1.0 PiB". std and no_std still pick the same unit.

Tests: two new tests (rounding_carries_into_next_unit, rounding_carry_honors_precision) plus the updated one fail on master and pass here. cargo test (default, --no-default-features, --all-features), clippy, and cargo +nightly fmt --check are clean. I also ran 2M random sizes through iec() and si() and checked that no output shows a number >= the unit.

1,048,575 B is 1023.999 KiB and was shown as "1024.0 KiB". After
picking the unit, check whether the value rounds to a whole unit at the
requested precision and, if so, show it in the next unit ("1.0 MiB").

Fixes bytesize-rs#212

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.

Display shows "1024.0 KiB" instead of "1.0 MiB" when the value rounds up

1 participant