fix: skip 0 for rounding

This commit is contained in:
Ankush Menat 2023-03-13 14:14:17 +05:30
parent 02168174c1
commit dd2ac72a9a
3 changed files with 6 additions and 0 deletions

View file

@ -189,6 +189,7 @@ function _round(num, precision, rounding_method) {
r = d ? r / m : r;
return is_negative ? -r : r;
} else if (rounding_method == "Banker's Rounding") {
if (num == 0) return 0.0;
precision = cint(precision);
let multiplier = Math.pow(10, precision);

View file

@ -63,6 +63,7 @@ from frappe.utils.data import (
now_datetime,
nowtime,
pretty_date,
rounded,
to_timedelta,
validate_python_code,
)
@ -1094,6 +1095,7 @@ class TestRounding(FrappeTestCase):
def test_bankers_rounding(self):
rounding_method = "Banker's Rounding"
self.assertEqual(rounded(0, 0, rounding_method=rounding_method), 0)
self.assertEqual(flt("0.5", 0, rounding_method=rounding_method), 0)
self.assertEqual(flt("0.3", rounding_method=rounding_method), 0.3)

View file

@ -1117,6 +1117,9 @@ def _round_away_from_zero(num, precision):
def _bankers_rounding(num, precision):
if num == 0:
return 0.0
multiplier = 10**precision
num = round(num * multiplier, 12)