Fix `precisedelta` rounding by dangillet · Pull Request #254 · python-humanize/humanize
and others added 6 commits
%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.
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
changed the title
Precisedelta rounding
Fix precisedelta rounding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters