From 1ea0e7a56c01fec38dff897bdb9dffa4610ad4fb Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 3 Dec 2024 10:14:13 -0700 Subject: [PATCH] Et2Number formatting fixes - fix issues with multiple . as thousands separator changed the number on blur. eg: 123.456.789 was changed to 123.456,78 - add exception for N.A. numbers entered when "," is decimal separator eg: 1.5 assume user meant 1,5 not 1500 or 15. Precision is taken into account so if precision=3, assume 1.234 is 1,234 not 1.234,000. --- api/js/etemplate/Et2Textbox/Et2Number.ts | 13 +++++- .../Et2Textbox/test/Et2Number.test.ts | 40 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/api/js/etemplate/Et2Textbox/Et2Number.ts b/api/js/etemplate/Et2Textbox/Et2Number.ts index 61db962ddd..bfb4c2a1df 100644 --- a/api/js/etemplate/Et2Textbox/Et2Number.ts +++ b/api/js/etemplate/Et2Textbox/Et2Number.ts @@ -229,7 +229,18 @@ export class Et2Number extends Et2Textbox // Remove separator so parseFloat works if(typeof val === 'string') { - val = val.replace(this.thousandsSeparator, "").replace(",", '.'); + // Special exception if someone is entering a decimal using . even though their preference is , (N.A. number in Europe) + // Only 1 ".", no thousands separator and if precision is set decimal places must match + if(this.decimalSeparator != "." && val.indexOf(".") == val.lastIndexOf(".") && !val.includes(",") && + (typeof this.precision == "undefined" || typeof this.precision != "undefined" && this.precision == val.length - val.indexOf(".") - 1) + ) + { + // Leave it + } + else + { + val = val.replaceAll(this.thousandsSeparator, "").replace(",", '.'); + } } if(typeof this.precision !== 'undefined') diff --git a/api/js/etemplate/Et2Textbox/test/Et2Number.test.ts b/api/js/etemplate/Et2Textbox/test/Et2Number.test.ts index fafd967815..66ea78f028 100644 --- a/api/js/etemplate/Et2Textbox/test/Et2Number.test.ts +++ b/api/js/etemplate/Et2Textbox/test/Et2Number.test.ts @@ -122,5 +122,45 @@ describe("Number widget", () => }); }); }); + +describe("'.' as thousands separator", () => +{ + // Setup run before each test + beforeEach(async() => + { + await before(); + element.thousandsSeparator = "."; + element.decimalSeparator = ","; + element.requestUpdate(); + }) + + const tests = [ + {args: ["1234567890"], expected: 1234567890}, + {args: ["123.4567.890"], expected: 1234567890}, // This one is wrongly entered by user + {args: ["123.456.789"], expected: 123456789}, + {args: ["1234567.890"], expected: 1234567890}, + {args: ["1234567890,0"], expected: 1234567890}, + {args: ["123.456.789,0"], expected: 123456789}, + {args: ["1234567890,1"], expected: 1234567890.1}, + {args: ["123.456.7890,1"], expected: 1234567890.1}, + {args: ["1.234.567.890,1"], expected: 1234567890.1}, + {args: ["1.234567890,1"], expected: 1234567890.1}, + {args: ["1.234"], expected: 1234}, + {args: ["1.234,5"], expected: 1234.5}, + {args: ["1,234"], expected: 1.234}, + {args: ["1,234.5"], expected: 1.2345}, + ] + tests.forEach(({args, expected}) => + { + it("Handles " + args[0], () => + { + element.value = args[0]; + assert.equal(element.valueAsNumber, expected, "Failed on setting .value"); + + element.blur(); + assert.equal(element.valueAsNumber, expected, "Failed on blur"); + }); + }); +}); // // inputBasicTests(before, "I'm a good test value", "input"); \ No newline at end of file