How to parse formated Number() based on local regional settings in Javascript

64 Views Asked by At

I wanted to parse a formatted number string accepting comma space, dot as thousand separator and comma and dot as decimal separator

examples Input = 1 2345,6 locale -swedish //output 12345.6

Input = 1.2345,6 -danish //output 12345.6 Input = 1,2345.6 - english //output 12345.6

1

There are 1 best solutions below

0
Ahmet Remzi EKMEKCI On

OK, first we can write a parser to convert given number according to the locale;

if (locale === 'turkish') {
    num = localized_num.replace(',', '');
} else if (locale === 'swedish') {
    num = localized_num.replace(/\s/g, '').replace(',', '.');
}

then you can test it with converting the given number to specific locale again. sample code:

Intl.NumberFormat("tr-TR").format(num) === localized_num