Merge pull request #22441 from blaggacao/develop
fix: `flt` with trailing zeros
This commit is contained in:
commit
091c6ca49f
4 changed files with 31 additions and 8 deletions
|
|
@ -47,6 +47,17 @@ context("Control Currency", () => {
|
|||
df_options: { precision: 0 },
|
||||
blur_expected: "10",
|
||||
},
|
||||
{
|
||||
input: "10.000",
|
||||
number_format: "#.###,##",
|
||||
df_options: { precision: 0 },
|
||||
blur_expected: "10.000",
|
||||
},
|
||||
{
|
||||
input: "10.000",
|
||||
number_format: "#.###,##",
|
||||
blur_expected: "10.000,00",
|
||||
},
|
||||
{
|
||||
input: "10.101",
|
||||
df_options: { precision: "" },
|
||||
|
|
@ -61,6 +72,7 @@ context("Control Currency", () => {
|
|||
.then((frappe) => {
|
||||
frappe.boot.sysdefaults.currency = test_case.currency;
|
||||
frappe.boot.sysdefaults.currency_precision = test_case.default_precision ?? 2;
|
||||
frappe.boot.sysdefaults.number_format = test_case.number_format ?? "#,###.##";
|
||||
});
|
||||
|
||||
get_dialog_with_currency(test_case.df_options).as("dialog");
|
||||
|
|
|
|||
|
|
@ -83,6 +83,23 @@ context("Control Float", () => {
|
|||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
// '.' is the parseFloat's decimal separator
|
||||
number_format: "#.###,##",
|
||||
values: [
|
||||
{
|
||||
input: "12.345",
|
||||
blur_expected: "12.345,000",
|
||||
focus_expected: "12345",
|
||||
},
|
||||
{
|
||||
// parseFloat would reduce 12,340 to 12,34 if this string was ever to be parsed
|
||||
input: "12.340",
|
||||
blur_expected: "12.340,000",
|
||||
focus_expected: "12340",
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ context("Rounding behaviour", () => {
|
|||
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("0.3", null, null, rounding_method)).eq(0.3);
|
||||
|
||||
expect(flt("1.5", 0, null, rounding_method)).eq(2);
|
||||
|
||||
|
|
|
|||
|
|
@ -8,12 +8,7 @@ if (!window.frappe) window.frappe = {};
|
|||
function flt(v, decimals, number_format, rounding_method) {
|
||||
if (v == null || v == "") return 0;
|
||||
|
||||
if (!(typeof v === "number" || String(parseFloat(v)) == v)) {
|
||||
// cases in which this block should not run
|
||||
// 1. 'v' is already a number
|
||||
// 2. v is already parsed but in string form
|
||||
// if (typeof v !== "number") {
|
||||
|
||||
if (typeof v !== "number") {
|
||||
v = v + "";
|
||||
|
||||
// strip currency symbol if exists
|
||||
|
|
@ -29,7 +24,6 @@ function flt(v, decimals, number_format, rounding_method) {
|
|||
if (isNaN(v)) v = 0;
|
||||
}
|
||||
|
||||
v = parseFloat(v);
|
||||
if (decimals != null) return _round(v, decimals, rounding_method);
|
||||
return v;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue