diff --git a/addons/web/static/src/views/fields/parsers.js b/addons/web/static/src/views/fields/parsers.js index ec986f66535..908ee41fcfe 100644 --- a/addons/web/static/src/views/fields/parsers.js +++ b/addons/web/static/src/views/fields/parsers.js @@ -44,8 +44,15 @@ function parseNumber(value, options = {}) { value = Math.trunc(value); } } else { + // A whitespace thousands separator is equivalent to any whitespace character. + // E.g. "1 000 000" should be parsed as 1000000 even if the + // thousands separator is nbsp. + const thousandsSepRegex = options.thousandsSep.match(/\s+/) + ? /\s+/g + : new RegExp(escapeRegExp(options.thousandsSep), "g") || ","; + // a number can have the thousand separator multiple times. ex: 1,000,000.00 - value = value.replaceAll(new RegExp(escapeRegExp(options.thousandsSep), "g") || ",", ""); + value = value.replaceAll(thousandsSepRegex, ""); // a number only have one decimal separator value = value.replace(new RegExp(escapeRegExp(options.decimalPoint), "g") || ".", "."); } diff --git a/addons/web/static/tests/views/fields/parsers_tests.js b/addons/web/static/tests/views/fields/parsers_tests.js index ba1c2487c52..68dfe031298 100644 --- a/addons/web/static/tests/views/fields/parsers_tests.js +++ b/addons/web/static/tests/views/fields/parsers_tests.js @@ -11,6 +11,7 @@ import { import { session } from "@web/session"; import { defaultLocalization } from "@web/../tests/helpers/mock_services"; import { patchWithCleanup } from "@web/../tests/helpers/utils"; +import { nbsp } from "@web/core/utils/strings"; function expectInvalidNumberError(assert, func, value, options) { let message = `${func.name} fails on value: "${value}"`; @@ -48,6 +49,11 @@ QUnit.module("Fields", (hooks) => { patchWithCleanup(localization, { decimalPoint: ",", thousandsSep: false }); assert.strictEqual(parseFloat("1234,567"), 1234.567); + + patchWithCleanup(localization, { decimalPoint: ",", thousandsSep: nbsp }); + assert.strictEqual(parseFloat("9 876,543"), 9876.543); + assert.strictEqual(parseFloat("1 234 567,89"), 1234567.89); + assert.strictEqual(parseFloat(`98${nbsp}765 432,1`), 98765432.1); }); QUnit.test("parseFloatTime", function (assert) {