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
Integer conversion inconsistent #47274
Comments
This issue is probably older than I am, and was amazed to discover it in
python:
Python 2.5.2 (r252:60911, May 7 2008, 15:19:09)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
>>> int(float("-23.15"))
-23
>>> int(float("-23.65"))
-23
>>> int(float("24.9"))
24
>>> int(float("24.4"))
24
Is this by design? What is the python way of obtaining a correct result? |
I don't see any problem with that result? Why do you consider the result Notice that int conversion of floats truncates them, by definition. |
Hello, |
Sorry for not writing completely above - python does neither, it rounds |
I see. There is no such thing as a "correct" conversion from real If you want rounding, use the round builtin. |
This is exactly what int() is supposed to do. For other kinds of from decimal import Decimal
>>> Decimal('-23.15').to_integral(ROUND_FLOOR)
Decimal("-24")
>>> Decimal('-23.15').to_integral(ROUND_CEILING)
Decimal("-23")
>>> Decimal('-23.15').to_integral(ROUND_DOWN)
Decimal("-23")
>>> Decimal('-23.15').to_integral(ROUND_HALF_EVEN)
Decimal("-23") |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: