From 97ca92e3d18771e0e782c2ccbdfd752a6b106514 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 10 Mar 2023 14:57:15 +0530 Subject: [PATCH] refactor: change rounding method names (#20299) These are easy to understand. Added third method for corrected banker's rounding. --- cypress/integration/rounding.js | 2 +- frappe/core/doctype/system_settings/system_settings.json | 6 +++--- frappe/public/js/frappe/utils/number_format.js | 6 +++--- frappe/tests/test_utils.py | 6 +++--- frappe/utils/data.py | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cypress/integration/rounding.js b/cypress/integration/rounding.js index 647b32a6a5..daad2def8f 100644 --- a/cypress/integration/rounding.js +++ b/cypress/integration/rounding.js @@ -8,7 +8,7 @@ context("Rounding behaviour", () => { cy.window() .its("flt") .then((flt) => { - let rounding_method = "Rounding Half Away From Zero"; + let rounding_method = "Commercial Rounding"; expect(flt("0.5", 0, null, rounding_method)).eq(1); expect(flt("0.3", null, null, rounding_method)).eq(0.3); diff --git a/frappe/core/doctype/system_settings/system_settings.json b/frappe/core/doctype/system_settings/system_settings.json index 2c9e92d943..642e454717 100644 --- a/frappe/core/doctype/system_settings/system_settings.json +++ b/frappe/core/doctype/system_settings/system_settings.json @@ -523,17 +523,17 @@ "label": "Login with email link expiry (in minutes)" }, { - "default": "Round Half Even", + "default": "Banker's Rounding (legacy)", "fieldname": "rounding_method", "fieldtype": "Select", "label": "Rounding Method", - "options": "Round Half Even\nRounding Half Away From Zero" + "options": "Banker's Rounding (legacy)\nCommercial Rounding" } ], "icon": "fa fa-cog", "issingle": 1, "links": [], - "modified": "2023-03-06 11:31:19.144956", + "modified": "2023-03-10 12:23:45.248125", "modified_by": "Administrator", "module": "Core", "name": "System Settings", diff --git a/frappe/public/js/frappe/utils/number_format.js b/frappe/public/js/frappe/utils/number_format.js index 2c457e75fe..52f9ac251b 100644 --- a/frappe/public/js/frappe/utils/number_format.js +++ b/frappe/public/js/frappe/utils/number_format.js @@ -175,11 +175,11 @@ function get_number_format_info(format) { function _round(num, precision, rounding_method) { rounding_method = - rounding_method || frappe.boot.sysdefaults.rounding_method || "Round Half Even"; + rounding_method || frappe.boot.sysdefaults.rounding_method || "Banker's Rounding (legacy)"; let is_negative = num < 0 ? true : false; - if (rounding_method == "Round Half Even") { + if (rounding_method == "Banker's Rounding (legacy)") { var d = cint(precision); var m = Math.pow(10, d); var n = +(d ? Math.abs(num) * m : Math.abs(num)).toFixed(8); // Avoid rounding errors @@ -188,7 +188,7 @@ function _round(num, precision, rounding_method) { var r = !precision && f == 0.5 ? (i % 2 == 0 ? i : i + 1) : Math.round(n); r = d ? r / m : r; return is_negative ? -r : r; - } else if (rounding_method == "Rounding Half Away From Zero") { + } else if (rounding_method == "Commerical Rounding") { if (num == 0) return 0.0; let digits = cint(precision); diff --git a/frappe/tests/test_utils.py b/frappe/tests/test_utils.py index ce13ee65cd..52e91b9500 100644 --- a/frappe/tests/test_utils.py +++ b/frappe/tests/test_utils.py @@ -1007,7 +1007,7 @@ class TestTBSanitization(FrappeTestCase): class TestRounding(FrappeTestCase): - @change_settings("System Settings", {"rounding_method": "Rounding Half Away From Zero"}) + @change_settings("System Settings", {"rounding_method": "Commercial Rounding"}) def test_normal_rounding(self): self.assertEqual(flt("what"), 0) @@ -1041,7 +1041,7 @@ class TestRounding(FrappeTestCase): self.assertEqual(flt(-0.15, 1), -0.2) def test_normal_rounding_as_argument(self): - rounding_method = "Rounding Half Away From Zero" + rounding_method = "Commercial Rounding" self.assertEqual(flt("0.5", 0, rounding_method=rounding_method), 1) self.assertEqual(flt("0.3", rounding_method=rounding_method), 0.3) @@ -1073,7 +1073,7 @@ class TestRounding(FrappeTestCase): self.assertEqual(flt(-1.25, 1, rounding_method=rounding_method), -1.3) self.assertEqual(flt(-0.15, 1, rounding_method=rounding_method), -0.2) - @change_settings("System Settings", {"rounding_method": "Rounding Half Away From Zero"}) + @change_settings("System Settings", {"rounding_method": "Commercial Rounding"}) @given(st.decimals(min_value=-1e8, max_value=1e8), st.integers(min_value=-2, max_value=4)) def test_normal_rounding_property(self, number, precision): with localcontext() as ctx: diff --git a/frappe/utils/data.py b/frappe/utils/data.py index eeae737144..8a6037d718 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -1055,12 +1055,12 @@ def rounded(num, precision=0, rounding_method=None): precision = cint(precision) rounding_method = ( - rounding_method or frappe.get_system_settings("rounding_method") or "Round Half Even" + rounding_method or frappe.get_system_settings("rounding_method") or "Banker's Rounding (legacy)" ) - if rounding_method == "Round Half Even": + if rounding_method == "Banker's Rounding (legacy)": return _round_half_even(num, precision) - elif rounding_method == "Rounding Half Away From Zero": + elif rounding_method == "Commercial Rounding": return _round_away_from_zero(num, precision) else: frappe.throw(