Apr-12-2020, 08:46 AM
It's the default rounding mode in IEEE-754 floating point https://en.wikipedia.org/wiki/IEEE_754#R...to_nearest
If you want to use "ties round away from zero" instead, I'd probably just code my own as you've done. Another solution would be to use the decimal module. But using that and setting up the context to use the alternate rounding isn't compact. Fine if you're doing a lot of math, but not something I'd bother with for a single calculation. Something like this:
If you want to use "ties round away from zero" instead, I'd probably just code my own as you've done. Another solution would be to use the decimal module. But using that and setting up the context to use the alternate rounding isn't compact. Fine if you're doing a lot of math, but not something I'd bother with for a single calculation. Something like this:
from decimal import * print(Decimal(0.5).to_integral_value()) # With default context, will round to even and print 0 setcontext(Context(rounding=ROUND_HALF_UP)) print(Decimal(0.5).to_integral_value()) # With new context, will round up and print 1The decimal module has several different rounding methods. Rounding modes in decimal