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.
This commit is contained in:
nathan 2024-12-03 10:14:13 -07:00
parent 2851da1ca7
commit 049eafe91d
2 changed files with 52 additions and 1 deletions

View File

@ -224,7 +224,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')

View File

@ -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");