-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configurable rounding methods (v14 port) (#20330)
* feat: configurable rounding methods * feat: Allow specifying rounding method in `flt` * feat: implement custom rounding in JS * fix(UX): Warn about changing rounding method * refactor: split rounding methods in functions * refactor: change rounding method names (#20299) These are easy to understand. Added third method for corrected banker's rounding. * chore: typo * fix: corrected banker's rounding closes #19570 * fix: Make corrected bankers rounding default method * fix: skip 0 for rounding * Revert "fix: Make corrected bankers rounding default method" This reverts commit 709edf1. * fix: add patch for setting current method as default
- Loading branch information
Showing
11 changed files
with
432 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
context("Rounding behaviour", () => { | ||
before(() => { | ||
cy.login(); | ||
cy.visit("/app/"); | ||
}); | ||
|
||
it("Commercial Rounding", () => { | ||
cy.window() | ||
.its("flt") | ||
.then((flt) => { | ||
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); | ||
|
||
expect(flt("1.5", 0, null, rounding_method)).eq(2); | ||
|
||
// positive rounding to integers | ||
expect(flt(0.4, 0, null, rounding_method)).eq(0); | ||
expect(flt(0.5, 0, null, rounding_method)).eq(1); | ||
expect(flt(1.455, 0, null, rounding_method)).eq(1); | ||
expect(flt(1.5, 0, null, rounding_method)).eq(2); | ||
|
||
// negative rounding to integers | ||
expect(flt(-0.5, 0, null, rounding_method)).eq(-1); | ||
expect(flt(-1.5, 0, null, rounding_method)).eq(-2); | ||
|
||
// negative precision i.e. round to nearest 10th | ||
expect(flt(123, -1, null, rounding_method)).eq(120); | ||
expect(flt(125, -1, null, rounding_method)).eq(130); | ||
expect(flt(134.45, -1, null, rounding_method)).eq(130); | ||
expect(flt(135, -1, null, rounding_method)).eq(140); | ||
|
||
// positive multiple digit rounding | ||
expect(flt(1.25, 1, null, rounding_method)).eq(1.3); | ||
expect(flt(0.15, 1, null, rounding_method)).eq(0.2); | ||
expect(flt(2.675, 2, null, rounding_method)).eq(2.68); | ||
|
||
// negative multiple digit rounding | ||
expect(flt(-1.25, 1, null, rounding_method)).eq(-1.3); | ||
expect(flt(-0.15, 1, null, rounding_method)).eq(-0.2); | ||
}); | ||
}); | ||
|
||
it("Banker's Rounding", () => { | ||
cy.window() | ||
.its("flt") | ||
.then((flt) => { | ||
let rounding_method = "Banker's Rounding"; | ||
|
||
expect(flt("0.5", 0, null, rounding_method)).eq(0); | ||
expect(flt("0.3", null, rounding_method)).eq(0.3); | ||
|
||
expect(flt("1.5", 0, null, rounding_method)).eq(2); | ||
|
||
// positive rounding to integers | ||
expect(flt(0.4, 0, null, rounding_method)).eq(0); | ||
expect(flt(0.5, 0, null, rounding_method)).eq(0); | ||
expect(flt(1.455, 0, null, rounding_method)).eq(1); | ||
expect(flt(1.5, 0, null, rounding_method)).eq(2); | ||
|
||
// negative rounding to integers | ||
expect(flt(-0.5, 0, null, rounding_method)).eq(0); | ||
expect(flt(-1.5, 0, null, rounding_method)).eq(-2); | ||
|
||
// negative precision i.e. round to nearest 10th | ||
expect(flt(123, -1, null, rounding_method)).eq(120); | ||
expect(flt(125, -1, null, rounding_method)).eq(120); | ||
expect(flt(134.45, -1, null, rounding_method)).eq(130); | ||
expect(flt(135, -1, null, rounding_method)).eq(140); | ||
|
||
// positive multiple digit rounding | ||
expect(flt(1.25, 1, null, rounding_method)).eq(1.2); | ||
expect(flt(0.15, 1, null, rounding_method)).eq(0.2); | ||
expect(flt(2.675, 2, null, rounding_method)).eq(2.68); | ||
expect(flt(-2.675, 2, null, rounding_method)).eq(-2.68); | ||
|
||
// negative multiple digit rounding | ||
expect(flt(-1.25, 1, null, rounding_method)).eq(-1.2); | ||
expect(flt(-0.15, 1, null, rounding_method)).eq(-0.2); | ||
|
||
// Nearest number and not even (the default behaviour) | ||
expect(flt(0.5, 0, null, rounding_method)).eq(0); | ||
expect(flt(1.5, 0, null, rounding_method)).eq(2); | ||
expect(flt(2.5, 0, null, rounding_method)).eq(2); | ||
expect(flt(3.5, 0, null, rounding_method)).eq(4); | ||
|
||
expect(flt(0.05, 1, null, rounding_method)).eq(0.0); | ||
expect(flt(1.15, 1, null, rounding_method)).eq(1.2); | ||
expect(flt(2.25, 1, null, rounding_method)).eq(2.2); | ||
expect(flt(3.35, 1, null, rounding_method)).eq(3.4); | ||
|
||
expect(flt(-0.5, 0, null, rounding_method)).eq(0); | ||
expect(flt(-1.5, 0, null, rounding_method)).eq(-2); | ||
expect(flt(-2.5, 0, null, rounding_method)).eq(-2); | ||
expect(flt(-3.5, 0, null, rounding_method)).eq(-4); | ||
|
||
expect(flt(-0.05, 1, null, rounding_method)).eq(0.0); | ||
expect(flt(-1.15, 1, null, rounding_method)).eq(-1.2); | ||
expect(flt(-2.25, 1, null, rounding_method)).eq(-2.2); | ||
expect(flt(-3.35, 1, null, rounding_method)).eq(-3.4); | ||
}); | ||
}); | ||
}); |
Empty file.
12 changes: 12 additions & 0 deletions
12
frappe/core/doctype/system_settings/patches/set_default_rounding_method.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import frappe | ||
from frappe.database.database import savepoint | ||
|
||
|
||
def execute(): | ||
"""set default rounding method""" | ||
|
||
with savepoint(Exception): | ||
settings = frappe.get_doc("System Settings") | ||
settings.rounding_method = "Banker's Rounding (legacy)" | ||
settings.flag.ignore_mandatory = True | ||
settings.save(ignore_version=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.