20

I'm working on an application, that displays numbers according to the user's configuration. Everything works as expected, except when I try with numbers less than 10000,in Chrome, with the following locale: "es-AR". Any ideas?

Chrome:

enter image description here

Firefox:

enter image description here

Edge: enter image description here

console.log( (10000).toLocaleString("es-AR") );
console.log( (9999).toLocaleString("es-AR") );
console.log( (9999).toLocaleString("en-US") );
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
JS5
  • 729
  • 7
  • 17

3 Answers3

15

This is the intended behaviour for Spanish locales according to the latest CLDR data. Specifically the "Minimum Grouping Digits" are set to 2 according to the CLDR survey tool.

This means that that the grouping separator is only used when there would be 2 or more digits before it.

Apparently Firefox and Edge either uses an older version of that data or don't have support for that field yet.

The feature was introduced in CLDR 26, which was released in 2014, but enhancements like this take quite a while to make their way through the software stack.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • This explains the bug (with the typical "it's by design" excuse), but is there a built-in workaround? Or should one just rewrite the number formating function to fix the thousand separator placement? – Alejandro Jul 22 '21 at 21:40
  • @Alejandro: localization support getting better is a feature. If you insist that it's a bug, then maybe you should switch to C and the `itoa` family of methods. – Joachim Sauer Jul 28 '21 at 12:14
  • That's not a better behavior. While this implements some "specification", that in turn reflects nothing like real life. Being a native spanish speaker, I'm aware of no spanish variant where such notation would be seen as correct. In fact, I only became aware of this weird behavior because of a piece of code I've written that uses it, when QA came back to me with "it's working fine, but let's make the thousands separator uniform, now it works on some values and not in others". I stand my comment that it's a bug, if not in the implementation, in the specs, because it doesn't matches real life. – Alejandro Aug 23 '21 at 22:24
2

Minimum Grouping Digits for Spain's Spanish language, according CLDR, is indeed two.

Full language specifications can be found in GitHub.

Interestingly enough, using always a minimum of two grouping digits is in conflict with the RAE rules. They state that this recommendation doesn't apply to all contexts, mentioning as specific exceptions accountancy and any context where it can involve a security risk.

JesusIniesta
  • 10,412
  • 1
  • 35
  • 28
  • Any source for the *RAE rules*? – Madacol Dec 03 '19 at 23:42
  • 2
    @Madacol. RAE source: http://lema.rae.es/dpd/srv/search?id=PxrAnmVfND6FK0uGdT *" Esta recomendación no debe aplicarse en documentos contables ni en ningún tipo de escrito en que la separación arriesgue la seguridad."* – JesusIniesta Dec 10 '19 at 13:15
  • _Para facilitar la lectura de estos números, cuando constan de más de cuatro cifras se recomienda separar estas mediante espacios por grupos de tres, contando de derecha a izquierda: 52 345, 6 462 749._ As I understand, RAE recommendation is using whitespace rather dan dot for group separator – laconbass May 18 '21 at 14:21
1

Given that the spec forces this behavior and that real-life experience is that this is sometimes reported as a bug that needs fixing, here is a simple workaround as requested by the original poster. It's as dirty as detecting four integer digits and adding the thousands separator manually (and could be done many better ways).

var decimals=2;

value=value.toLocaleString('es', {minimumFractionDigits: decimals, maximumFractionDigits: decimals});

//fix spanish thousands separator for <10.000
if(value.indexOf(',')==4 || (decimals==0 && value.length==4)){
    value=value.substr(0,1)+'.'+value.substr(1);
}
guidod
  • 996
  • 9
  • 13