Skip to content

Fix naturalsize() truncating non-integer bytes below the base - #507

Open
ekanshul wants to merge 1 commit into
python-humanize:mainfrom
ekanshul:fix-naturalsize-fraction
Open

ekanshul wants to merge 1 commit into
python-humanize:mainfrom
ekanshul:fix-naturalsize-fraction

Conversation

@ekanshul

@ekanshul ekanshul commented Nov 5, 2026

Copy link
Copy Markdown

Fixes #459

naturalsize(1.5) returned '1 Bytes' — below the base the value was truncated with int(), while from 1 kB upwards the fraction was kept via format. The singular check also compared the untruncated value with 1, so 1.5 showed as "1 Bytes".

Fix: format non-integer values below the base with the caller's format string — 1.5 Bytes (decimal), 1.5B with gnu=True — keeping integer behavior unchanged (300 Bytes, 1 Byte). The singular "1 Byte" branch now requires an actual integer value.

Updated/added cases in tests/test_filesize.py. tests/test_filesize.py + tests/test_number.py — 321 passed locally.

Co-authored-by: CommandCodeBot noreply@commandcode.ai

Fixes python-humanize#459

Below-base values used int(bytes_), so naturalsize(1.5) returned
'1 Bytes' while larger units kept the fraction. Format
non-integers with the format string (1.5 Bytes / 1.5B); keep
integer behavior ('300 Bytes', '1 Byte') unchanged.

Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
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.

naturalsize() truncates non-integer byte counts below the base ("1 Bytes")

1 participant