Feat/round half up
Override built-in round()
so that it always behave as ROUND_HALF_UP.
In python 2
for i in (-1.5, -0.5, 0.5, 1.5):
print i, round_orig(i), round(i)
-1.5, -2.0, -2.0
-0.5, -1.0, -1.0
0.5, 1.0, 1.0
1.5, 2.0, 2.0
for i in (-.15, -.05, .05, .15):
print i, round_orig(i,1), round(i,1)
-0.15, -0.1, -0.2
-0.05, -0.1, -0.1
0.05, 0.1, 0.1
0.15, 0.1, 0.2
In python 3
for i in (-1.5, -0.5, 0.5, 1.5):
print(i, round_orig(i), round(i))
-1.5 -2 -2.0
-0.5 0 -1.0
0.5 0 1.0
1.5 2 2.0
for i in (-.15, -.05, .05, .15):
print(i, round_orig(i,1), round(i,1))
-0.15 -0.1 -0.2
-0.05 -0.1 -0.1
0.05 0.1 0.1
0.15 0.1 0.2
This behaviour is different than MySQL/MariaDB implementation for approximate-value numbers (like float/double).
mysql> SELECT ROUND(2.5), ROUND(CAST(2.5 AS DOUBLE));
+------------+----------------------------+
| ROUND(2.5) | ROUND(CAST(2.5 AS DOUBLE)) |
+------------+----------------------------+
| 3 | 2 |
+------------+----------------------------+
You can see MariaDB implementation that uses libc's rint()
, not round()
.