Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

precisedelta unusual results when rounding #30

Open
ralkire opened this issue Jun 28, 2022 · 2 comments
Open

precisedelta unusual results when rounding #30

ralkire opened this issue Jun 28, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@ralkire
Copy link

ralkire commented Jun 28, 2022

What did you do?

If I pass 32399 seconds to precisedelta (8 hours 59 minutes 59 seconds) and set minimum time to minutes, it does not round up to hours. That is, it will round 59 minutes 59 seconds to 60 minutes but doesn't take the step further to round 60 minutes to hours.

What did you expect to happen?

'9 hours'

What actually happened?

'8 hours and 60 minutes'

What versions are you using?

  • OS: Debian 4.19
  • Python: 3.73
  • Humanize: 4.22

Please include code that reproduces the issue.

import humanize
humanize.precisedelta(32399, minimum_unit='minutes', format='%0.0f')
@hugovk hugovk added the bug Something isn't working label Jun 28, 2022
@eldipa
Copy link
Contributor

eldipa commented Jul 7, 2022

Hi @ralkire, let me show you step by step what is happening.

>>> import humanize
>>> humanize.precisedelta(32399)
'8 hours, 59 minutes and 59 seconds'

Indeed the 32399 is not "9 hours" but one second before. The idea of precisedelta is to preserve the precision and do not do any rounding unless it is explicitly requested.

Now, let's add minimum_unit:

>>> humanize.precisedelta(32399, minimum_unit='minutes')
'8 hours and 59.98 minutes'

Once again precisedelta preserves the precision. Because the delta cannot be represented by an integer number of minutes, precisedelta formats it as float. Remember that 32399 is not "9 hours" so doing a rounding would be incorrect.

Finally, let's add the format:

>>> humanize.precisedelta(32399, minimum_unit='minutes', format='%0.0f')
'8 hours and 60 minutes'

It looks unusual, yes, but why is happening. The issue is not in precisedelta but in the format '%0.0f'.
The 59.98 minutes is formatted with '%0.0f' and this is literally saying that the float number must be rounded to have 0 digits after the dot.

That's why 59.98 goes to 60. It is an artifact of the formatting, not of the calculation.

@webdeck
Copy link

webdeck commented Sep 1, 2023

Similar issue with seeing incorrect plurals when using a format of "%0.0f":

>>> humanize.precisedelta(91500)
'1 day, 1 hour and 25 minutes'
>>> humanize.precisedelta(91500, minimum_unit='hours')
'1 day and 1.42 hours'
>>> humanize.precisedelta(91500, minimum_unit='hours', format="%0.0f")
'1 day and 1 hours'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants