Skip to content

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

Open

Description

@uvrvuoon

On current main:

>>> import humanize
>>> humanize.naturalsize(1.5)
'1 Bytes'
>>> humanize.naturalsize(1.9)
'1 Bytes'
>>> humanize.naturalsize(1500)
'1.5 kB'

From 1 kB upwards the fraction is kept using format, but below the base the value is truncated with int(). The singular check also compares the untruncated value with 1, so 1.5 is shown as "1 Bytes".

In #239 you mentioned that "1.1 Bytes" would be better than "1 Bytes" for non-integer input, and that tests for non-integer input were missing.

Proposal:

  • Format non-integer values below the base with format, like larger units (e.g. "1.5 Bytes", and "1.5B" with gnu=True).
  • Keep integer input unchanged ("300 Bytes", "1 Byte").
  • Add tests for non-integer input.

Would you be happy with this? If so, we'd like to send a PR.

Activity

  1. manshi-n commented on Oct 20, 2026

    @manshi-n

    I'd like to contribute a fix for this issue. I'll add regression tests for fractional byte values while preserving the existing behavior for integer inputs. Is the proposed output format acceptable?

  2. uvrvuoon commented on Oct 21, 2026

    @uvrvuoon
    Author

    Thanks for your interest, @manshi-n! Our team (the issue author) is already working on this one, so we'd prefer to keep it on our side to avoid duplicate PRs. We'd be very happy to have your review once the PR is up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions