Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intl.NumberFormat shows incorrect result for es-ES currency formatting

I expect that output for es-ES and de-DE locale to be identical. (I asked my Spanish colleague to check his salary slip, and he confirms that there is indeed a decimal after thousand)

var number = 1000;
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
console.log(new Intl.NumberFormat('es-ES', { style: 'currency', currency: 'EUR' }).format(number));

Results:

> "1.000,00 €"
> "1000,00 €"
like image 815
maxcc00 Avatar asked Nov 22 '19 09:11

maxcc00


People also ask

What is Intl NumberFormat?

Intl.NumberFormat.prototype.format() Getter function that formats a number according to the locale and formatting options of this Intl.NumberFormat object. Intl.NumberFormat.prototype.formatToParts() Returns an Array of objects representing the number string in parts that can be used for custom locale-aware formatting.

How can I format euro?

If you are writing out an amount in euros, use the currency symbol or euro sign € . Note that the symbol € goes before the amount and that there is no space between them (e.g. € 50).

How do you format numbers in JavaScript?

The toFixed() method in JavaScript is used to format a number using fixed-point notation. It can be used to format a number with a specific number of digits to the right of the decimal. The toFixed() method is used with a number as shown in the above syntax using the '. ' operator.


1 Answers

According to the Real Academia Española, the standard form in Spanish is to separate numbers in groups of three, separated with whitespace, if the number has more than four digits, i.e. at least five (Source: https://www.rae.es/dpd/números, section 2.a, in Spanish).

Actually for numbers with five digits, your code gives me the same result for de-DE and es-ES, i.e. a separation with a dot:

let number2 = 10000
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number2));
console.log(new Intl.NumberFormat('es-ES', { style: 'currency', currency: 'EUR' }).format(number2));

gives me

10.000,00 €
10.000,00 €

The separation with dot seems not to be fully correct*, but that the grouping is not done for four-digit numbers seems to be in accord with the language rules of the RAE.

* Actually, also in German, according to the Duden you should use spaces rather than dots, see e.g. https://www.duden.de/sprachwissen/rechtschreibregeln/zahlen-und-ziffern (in German).

like image 147
Thomas Heinemann Avatar answered Oct 19 '22 23:10

Thomas Heinemann