fix: number format converting to decimalsversion-14
@@ -0,0 +1,93 @@ | |||||
context("Control Float", () => { | |||||
before(() => { | |||||
cy.login(); | |||||
cy.visit("/app/website"); | |||||
}); | |||||
function get_dialog_with_float() { | |||||
return cy.dialog({ | |||||
title: "Float Check", | |||||
fields: [ | |||||
{ | |||||
fieldname: "float_number", | |||||
fieldtype: "Float", | |||||
Label: "Float" | |||||
} | |||||
] | |||||
}); | |||||
} | |||||
it("check value changes", () => { | |||||
get_dialog_with_float().as("dialog"); | |||||
let data = get_data(); | |||||
data.forEach(x => { | |||||
cy.window() | |||||
.its("frappe") | |||||
.then(frappe => { | |||||
frappe.boot.sysdefaults.number_format = x.number_format; | |||||
}); | |||||
x.values.forEach(d => { | |||||
cy.get_field("float_number", "Float").clear(); | |||||
cy.fill_field("float_number", d.input, "Float").blur(); | |||||
cy.get_field("float_number", "Float").should( | |||||
"have.value", | |||||
d.blur_expected | |||||
); | |||||
cy.get_field("float_number", "Float").focus(); | |||||
cy.get_field("float_number", "Float").blur(); | |||||
cy.get_field("float_number", "Float").focus(); | |||||
cy.get_field("float_number", "Float").should( | |||||
"have.value", | |||||
d.focus_expected | |||||
); | |||||
}); | |||||
}); | |||||
}); | |||||
function get_data() { | |||||
return [ | |||||
{ | |||||
number_format: "#.###,##", | |||||
values: [ | |||||
{ | |||||
input: "364.87,334", | |||||
blur_expected: "36.487,334", | |||||
focus_expected: "36487.334" | |||||
}, | |||||
{ | |||||
input: "36487,334", | |||||
blur_expected: "36.487,334", | |||||
focus_expected: "36487.334" | |||||
}, | |||||
{ | |||||
input: "100", | |||||
blur_expected: "100,000", | |||||
focus_expected: "100" | |||||
} | |||||
] | |||||
}, | |||||
{ | |||||
number_format: "#,###.##", | |||||
values: [ | |||||
{ | |||||
input: "364,87.334", | |||||
blur_expected: "36,487.334", | |||||
focus_expected: "36487.334" | |||||
}, | |||||
{ | |||||
input: "36487.334", | |||||
blur_expected: "36,487.334", | |||||
focus_expected: "36487.334" | |||||
}, | |||||
{ | |||||
input: "100", | |||||
blur_expected: "100.000", | |||||
focus_expected: "100" | |||||
} | |||||
] | |||||
} | |||||
]; | |||||
} | |||||
}); |
@@ -1,4 +1,17 @@ | |||||
frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlInt { | frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlInt { | ||||
make_input() { | |||||
super.make_input(); | |||||
const change_handler = e => { | |||||
if (this.change) this.change(e); | |||||
else { | |||||
let value = this.get_input_value(); | |||||
this.parse_validate_and_set_in_model(value, e); | |||||
} | |||||
}; | |||||
// convert to number format on focusout since focus converts it to flt. | |||||
this.$input.on("focusout", change_handler); | |||||
} | |||||
parse(value) { | parse(value) { | ||||
value = this.eval_expression(value); | value = this.eval_expression(value); | ||||
return isNaN(parseFloat(value)) ? null : flt(value, this.get_precision()); | return isNaN(parseFloat(value)) ? null : flt(value, this.get_precision()); | ||||
@@ -8,7 +8,12 @@ if (!window.frappe) window.frappe = {}; | |||||
function flt(v, decimals, number_format) { | function flt(v, decimals, number_format) { | ||||
if (v == null || v == '') return 0; | if (v == null || v == '') return 0; | ||||
if (typeof v !== "number") { | |||||
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") { | |||||
v = v + ""; | v = v + ""; | ||||
// strip currency symbol if exists | // strip currency symbol if exists | ||||
@@ -25,6 +30,7 @@ function flt(v, decimals, number_format) { | |||||
v = 0; | v = 0; | ||||
} | } | ||||
v = parseFloat(v); | |||||
if (decimals != null) | if (decimals != null) | ||||
return _round(v, decimals); | return _round(v, decimals); | ||||
return v; | return v; | ||||