Skip to content

fix: carry suffix when rounding pushes file_size mantissa to base#761

Open
patchwright wants to merge 1 commit into
staticdev:mainfrom
patchwright:fix/file-size-rounding-carry
Open

fix: carry suffix when rounding pushes file_size mantissa to base#761
patchwright wants to merge 1 commit into
staticdev:mainfrom
patchwright:fix/file-size-rounding-carry

Conversation

@patchwright

Copy link
Copy Markdown

Problem

file_size(999999) returns '1000.0 KB' instead of '1.0 MB'.

The suffix is chosen from the raw byte count (before formatting), but the formatting pattern rounds the mantissa afterward. When rounding pushes the mantissa to base (e.g. 999,999 / 1000 = 999.999 → "1000.0"), the function keeps the smaller suffix.

Same with 999,999,999'1000.0 MB' (should be '1.0 GB').

Fix

After computing the formatted string, check whether float(formatted) >= base. If true and a larger suffix is available, step up one suffix. The formula byte_size / unit gives the value in the next unit because unit = base**(i+2) is also the conversion factor for suffix i+1.

Tests

Added three regression cases (KB→MB, MB→GB, GB→TB boundaries) to test_file_size.

The suffix is selected from the raw byte count before formatting.
When the formatted mantissa rounds up to base (e.g. 999,999 bytes is
999.999 KB, formatted as "1000.0 KB"), no carry logic existed.

After formatting, check whether the result is >= base; if so and a
larger suffix is available, step up one suffix so the output reads
"1.0 MB" instead of "1000.0 MB".

Adds three regression cases to test_file_size covering the boundary
for KB->MB, MB->GB, and GB->TB in decimal mode.
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.

1 participant