NSNumberFormatter.format produces strange output

Hello everebody!

I have some problem with NSNumberFormatter,unicode formats and locale.

I have:

public func build(locale: Locale?) -> NumberFormatter {
    let formatter = NumberFormatter()
    formatter.locale = locale
    formatter.currencySymbol = "$"
    formatter.negativeFormat = "#,##0.##"
    formatter.positiveFormat = "#,##0.##"
    formatter.usesGroupingSeparator = true
    formatter.currencyDecimalSeparator = "."
    formatter.currencyGroupingSeparator = " "
    return formatter
}

And for example let it be some formatters:

let enUs = build(locale: .init(identifier: "en_US"))
let ruRu = build(locale: .init(identifier: "ru_RU"))

So I need to get the same output in different locales: 10000 -> 10 000

But i can't do that with setting `format` properties.

And, if I set format to "#,##0.## ¤" (i just added ¤) then all thing will go fine - I get numbers with needed format with any locale.
I don't understant is it normal? Or it's some sort of bug? Why does NSNumberFormatter work thhis way?

Are you setting the numberStyle property of the formatter to one of the currency styles? Are you using localizesFormat to prevent the formatter from using the default separators and currency symbol from the locale?
The formatter does not know you are converting currencies.
Unless you add "¤" at the end of positive and negative format

So both will work:

Code Block
func build(locale: Locale?) -> NumberFormatter {
    let formatter = NumberFormatter()
    formatter.locale = locale
    formatter.currencySymbol = "$"
    formatter.negativeFormat = "#,##0.## ¤"   // Added currency indicator
    formatter.positiveFormat = "#,##0.## ¤"  
    formatter.usesGroupingSeparator = true
    formatter.currencyDecimalSeparator = "."
    formatter.currencyGroupingSeparator = " "
    return formatter
}


You get
10 000 $

Or

Code Block
func build(locale: Locale?) -> NumberFormatter {
    let formatter = NumberFormatter()
    formatter.locale = locale
    formatter.currencySymbol = "$"
    formatter.negativeFormat = "#,##0.##"   // In fact no more needed here
    formatter.positiveFormat = "#,##0.##"  
    formatter.usesGroupingSeparator = true
    formatter.currencyDecimalSeparator = "."
    formatter.groupingSeparator = " "
    return formatter
}


You get
10 000

NSNumberFormatter.format produces strange output
 
 
Q