◐ Shell
clean mode source ↗

Fix `precisedelta` rounding by dangillet · Pull Request #254 · python-humanize/humanize

and others added 6 commits

May 26, 2025 14:33
%d and %0.0f do not produce the same values. So we will first apply the
required formatting and turn the formatted string back into a float or int.

YEARS needs to be treated slightly differently as it needs to be formatted
with `intcomma`. We first check if the resulting value does not have any
fractional part and if not, we turn it into an int, so that it string
output is what a human would expect.

Added several unittests to highlight some of the differences between
using %d and %.0f as a format in precisedelta.
We only try to round the value based on the format provided if we are
dealing with the minimum_unit.
The logic about rounding due to formatting has been moved to
_quotient_and_remainder because this is where we have all the logic regarding
the minimum unit and suppress units.

Also removed _carry function. Instead use the same logic than was used for
calculating the `secs` based on the remaining amount of days. It is done
now also for `usecs` based on the `secs` remaining.

Add another block of logic to check after rounding if any units should be
promoted to a higher unit, in case of a rounding up.

hugovk

Although 1 month is 30.5 days on average, we want 31 days to be one
month, and not remainder of 0.5 days which would not be intuitive.

@hugovk hugovk changed the title Precisedelta rounding Fix precisedelta rounding

Aug 25, 2025

logpie added a commit to logpie/otto that referenced this pull request

Mar 23, 2026