Search code examples
iosswiftnumber-formatting

iOS13: NumberFormatter missing groupingSeparator when try to format 4 digits numbers with es_ES locale


I've updated my project to iOS 13 and I've realised that the price formatter I was using for formatting the prices has stopped working correctly.

The groupingSeparator, the symbol that groups the thousands, is missing for 4 digits numbers: from 1000 to 9999 when using a the Spanish locale es_ES.

Below there is a simple snipped to verify it. NumberFormatter seems to be working perfectly for en_US locale and for numbers higher than 9999 but whenever use es_ES locale and try to format a 4 digits number, the groupingSeparator (thousands separator) is missing, so instead of getting 1.000, I'm getting 1000.


let enFormatter = NumberFormatter()
enFormatter.locale = Locale(identifier: "en_US")
enFormatter.numberStyle = .decimal
enFormatter.string(from: 1000)
enFormatter.string(from: 9999)
enFormatter.string(from: 10000)
enFormatter.string(from: 100000)

let esFormatter = NumberFormatter()
esFormatter.locale = Locale(identifier: "es_ES")
esFormatter.numberStyle = .decimal
esFormatter.string(from: 1000)
esFormatter.string(from: 9999)
esFormatter.string(from: 10000)
esFormatter.string(from: 100000)

Just paste it into a playground and check the outputs.

Different outputs for en_US and es_ES NumberFormatter

Am I missing something?

So far I've reported the problem to Apple: FB7416623 https://feedbackassistant.apple.com/feedback/7416623 (not sure if the link works)

I'll keep this updated, but I would appreciate if anybody has more information about it.


Solution

  • Based on this article from the RAE (Real Academia de la Lengua Española: Royal Academy of the Spanish Language), and this other one from the Fundéu (Fundación del Español Urgente: Urgent Spanish Foundation), right now the rule is not to use the grouping separator for 4 digits number, and use a white space as a grouping separator for the rest of the cases. For example:

    1000  -> 1000
    15000 -> 15 000
    

    However ISO rules recommend to keep the grouping separator even for 4 digits numbers.