From 3ebba9502ae16daf40c10eda31fddc3244f457f5 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 7 Jun 2021 16:53:20 +0200 Subject: [PATCH] Handle more cases with decimal/thousands separator - ',' or '.' can be used as decimal separator (defined in the application configuration) - Remove thousands separator if it's detected as so (use regexp to match) --- .../filters/unlocalize_currency.js.coffee | 10 ++++-- .../unlocalize_currency_spec.js.coffee | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/admin/utils/filters/unlocalize_currency.js.coffee b/app/assets/javascripts/admin/utils/filters/unlocalize_currency.js.coffee index c6eca566ef..c8fe429bc3 100644 --- a/app/assets/javascripts/admin/utils/filters/unlocalize_currency.js.coffee +++ b/app/assets/javascripts/admin/utils/filters/unlocalize_currency.js.coffee @@ -6,11 +6,15 @@ angular.module("admin.utils").filter "unlocalizeCurrency", ()-> # used decimal and thousands separators from currency configuration decimal_separator = I18n.toCurrency(.1, {precision: 1, unit: ''}).substring(1,2) thousands_separator = I18n.toCurrency(1000, {precision: 1, unit: ''}).substring(1,2) + + # Replace comma used as a decimal separator and remplace by "." + if (price.match(/^[0-9]*(,{1})[0-9]{1,2}$/g)) + price = price.replace(",", ".") - if (price.length > 4) - # remove configured thousands separator if price is greater than 999 + # Remove configured thousands separator if it is actually a thousands separator + if (new RegExp("^([0-9]*(" + thousands_separator + "{1})[0-9]{3}[0-9\.,]*)*$", "g").test(price)) price = price.replaceAll(thousands_separator, '') - + if (decimal_separator == ",") price = price.replace(",", ".") diff --git a/spec/javascripts/unit/admin/filters/unlocalize_currency_spec.js.coffee b/spec/javascripts/unit/admin/filters/unlocalize_currency_spec.js.coffee index 7bba962c66..4b60c20de3 100644 --- a/spec/javascripts/unit/admin/filters/unlocalize_currency_spec.js.coffee +++ b/spec/javascripts/unit/admin/filters/unlocalize_currency_spec.js.coffee @@ -21,9 +21,18 @@ describe 'convert string to number with configurated currency', -> it "handle point as decimal separator", -> expect(filter("1.000")).toEqual 1.0 + it "also handle comma as decimal separator", -> + expect(filter("1,0")).toEqual 1.0 + it "also handle comma as decimal separator", -> expect(filter("1,00")).toEqual 1.0 + it "also handle comma as decimal separator", -> + expect(filter("11,00")).toEqual 11.0 + + it "handle comma as decimal separator but not confusing with thousands separator", -> + expect(filter("11,000")).toEqual 11000 + it "handle point as decimal separator and comma as thousands separator", -> expect(filter("1,000,000.00")).toEqual 1000000 @@ -47,10 +56,22 @@ describe 'convert string to number with configurated currency', -> it "handle comma as decimal separator", -> expect(filter("1,00")).toEqual 1.0 + + it "handle comma as decimal separator with one digit after the comma", -> + expect(filter("11,0")).toEqual 11.0 + + it "handle comma as decimal separator with two digit after the comma", -> + expect(filter("11,00")).toEqual 11.0 + + it "handle comma as decimal separator with three digit after the comma", -> + expect(filter("11,000")).toEqual 11.0 it "also handle point as decimal separator", -> expect(filter("1.00")).toEqual 1.0 + it "also handle point as decimal separator with integer part with two digits", -> + expect(filter("11.00")).toEqual 11.0 + it "handle point as decimal separator and final point as thousands separator", -> expect(filter("1.000.000,00")).toEqual 1000000 @@ -75,9 +96,21 @@ describe 'convert string to number with configurated currency', -> it "handle comma as decimal separator", -> expect(filter("1,00")).toEqual 1.0 + it "handle comma as decimal separator with one digit after the comma", -> + expect(filter("11,0")).toEqual 11.0 + + it "handle comma as decimal separator with two digit after the comma", -> + expect(filter("11,00")).toEqual 11.0 + + it "handle comma as decimal separator with three digit after the comma", -> + expect(filter("11,000")).toEqual 11.0 + it "also handle final point as decimal separator", -> expect(filter("1.00")).toEqual 1.0 + it "also handle final point as decimal separator with integer part with two digits", -> + expect(filter("11.00")).toEqual 11.0 + it "handle point as decimal separator and space as thousands separator", -> expect(filter("1 000 000,00")).toEqual 1000000