Is there a way to change the formatting by Intl.NumberFormat in JavaScript

Intl.NumberFormat (see Mozilla doc ) provides a good way in Javascript to format numbers in the current locale version:

 new Intl.NumberFormat().format(3400); // returns "3.400" for German locale 

But I could not find a way to cancel this formatting. There is something like

 new Intl.NumberFormat().unformat("3.400"); // returns 3400 for German locale 

Thanks for any help.

+9
source share
4 answers

I found a workaround:

 var separator = new Intl.NumberFormat().format(1111).replace(/1/g, ''); "3.400".replace(new RegExp('\\' + separator, 'g'), ''); 

Not the most pleasant solution, but it works :-)

If someone knows the best way to achieve this, feel free to post your answer.

Update
Here is a complete working number parser with a similar approach.

+6
source

what i have done so far is a multi-step approach that you can see in the code below. nf is a NumberFormat service. This function accepts a formatted number as well as the language used. Now we create a comparator, dividing 10k by 3, thereby guaranteeing the decimal and thousandth separators in a fixed position. Then remove the thousands separator and all other non-numeric characters, such as currency symbols. after that we replace the decimal separator with English and finally return the cast number.

  uf(number, locale) { let nf = this.nf({}, locale); let comparer = nf.format(10000 / 3); let thousandSeparator = comparer[1]; let decimalSeparator = comparer[5]; // remove thousand seperator let result = number.replace(thousandSeparator, '') // remove non-numeric signs except -> , . .replace(/[^\d.,-]/g, '') // replace original decimalSeparator with english one .replace(decimalSeparator, '.'); // return real number return Number(result); } 
+1
source

Here I created a function for the Reverse of format() function. This function will support reverse formatting in all locales.

 function reverseFormatNumber(val,locale){ var group = new Intl.NumberFormat(locale).format(1111).replace(/1/g, ''); var decimal = new Intl.NumberFormat(locale).format(1.1).replace(/1/g, ''); var reversedVal = val.replace(new RegExp('\\' + group, 'g'), ''); reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.'); return Number.isNaN(reversedVal)?0:reversedVal; } console.log(reverseFormatNumber('1,234.56','en')); console.log(reverseFormatNumber('1.234,56','de')); 
+1
source

Not sure about the relevance of this approach in terms of performance, but itโ€™s always good to have several options, so hereโ€™s another one:

 function getNumPrice(price, decimalpoint) { var p = price.split(decimalpoint); for (var i=0;i<p.length;i++) p[i] = p[i].replace(/\D/g,''); return p.join('.'); } 

In my case, the locale is set from PHP, so I get it using <?php echo cms_function_to_get_decimal_point();?> <?php echo cms_function_to_get_decimal_point();?> But, obviously, you can use the separation trick suggested in other answers instead .

0
source

Source: https://habr.com/ru/post/984231/


All Articles