-
Notifications
You must be signed in to change notification settings - Fork 149
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
naturalsize() contains a very common but serious programming bug #155
Comments
In which case are you finding a issue in the library? Your post opens with the rounding problem but it seems to work fine in the lib:
Thanks! |
If we're looking at binary sized (where the base is 1024) we never get anything like '1024.0 Bytes'. The value is truncated, rather then being rounded, so you get something slightly surprising at the limit: >>> import humanize
>>> humanize.naturalsize(1023.9999999999999, binary=True)
'1023 Bytes'
>>> humanize.naturalsize(1023.99999999999999, binary=True)
'1.0 KiB' That looks like a different problem from the one that StackOverflow post describes though (if it's a problem at all – I'm not sure if it is). |
Thanks @samueljsb! I just confirmed this weird behaviour in |
🚀 Development has moved to https://github.com/python-humanize/humanize 🚀 Please open new issues at https://github.com/python-humanize/humanize/issues |
I just finished writing a proper "human bytes" function. When I was done, I did a Google search and actually found this project but noticed that you have the same major bug that the majority of other programmers in the world have. Since you've created a library that people reuse, it's important that you fix the bug in the library.
Here's the bugged library file:
https://github.com/jmoiron/humanize/blob/e2b217f2407a4f16b99516caf8934a12b16d58de/src/humanize/filesize.py
Here's the code I posted on a throwaway stackoverflow account. It explains the bug and provides a solution. You can pick it apart and adapt it for your project. Good luck! :-)
https://stackoverflow.com/a/63839503/8874388
The text was updated successfully, but these errors were encountered: