From 5df8926378b3553e28fbd060c842439901473a01 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 28 Dec 2021 11:49:14 +0100 Subject: [PATCH] pythons round() behaves unexpected (for me at least) --- tools/math.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tools/math.py diff --git a/tools/math.py b/tools/math.py new file mode 100644 index 0000000..92ad48c --- /dev/null +++ b/tools/math.py @@ -0,0 +1,7 @@ +from decimal import Decimal, ROUND_HALF_UP +from .types import Numeric + + +def round_half_up(number: Numeric) -> int: + """ pythons round() rounds .5 to the *even* number; 0.5 == 0 """ + return int(Decimal(number).to_integral(ROUND_HALF_UP))