refactor: change rounding method names (#20299)
These are easy to understand. Added third method for corrected banker's rounding.
This commit is contained in:
parent
2223f97f88
commit
97ca92e3d1
5 changed files with 13 additions and 13 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue