Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Euro currency formatter in iOS moves symbol before/after number

I have a currency formatter for euros in my app. When the user initially sets it to euros from whatever other currency, the formatter displays it as €1000. When the app is restarted however it changes it to 1000 €, and sometimes it even ends up as €1000 €! Any idea what's going on here?

func formatAsCurrency(currencyCode: String)  -> String? {
    let currencyFormatter = NSNumberFormatter()
    let isWholeNumber: Bool = floor(self) == self
    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currencyFormatter.maximumFractionDigits = isWholeNumber ? 0 : 2
    currencyFormatter.minimumFractionDigits = isWholeNumber ? 0 : 2
    currencyFormatter.locale = NSLocale(localeIdentifier: currencyCode)

    if let currencyString = currencyFormatter.stringFromNumber(self) {
        return currencyString
    }

    return nil
}
like image 263
Chris Byatt Avatar asked Nov 24 '25 16:11

Chris Byatt


2 Answers

As lgor said, you want to use currencyCode rather than locale... Does this work as a replacement?

extension Double {

    /// Formats the receiver as a currency string using the specified three digit currencyCode. Currency codes are based on the ISO 4217 standard.
    func formatAsCurrency(currencyCode: String) -> String? {
        let currencyFormatter = NSNumberFormatter()
        currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        currencyFormatter.currencyCode = currencyCode
        currencyFormatter.maximumFractionDigits = floor(self) == self ? 0 : 2
        return currencyFormatter.stringFromNumber(self)
    }
}

300.00.formatAsCurrency("GBP")      // "£300"
129.92.formatAsCurrency("EUR")      // "€129.92"
(-532.23).formatAsCurrency("USD")   // "-$532.23"

It's also worth pointing out why you are seeing strange formatting behaviour when you modify the locale. By changing the locale, the formatter is applying different formatting rules based on that localisation.

Usually, you want to leave the locale set to its default value (NSLocale.currentLocale()), that way the format of the string will be localised to the users language. This is usually for things like differences in decimal and thousand separators

If you specifically want numbers formatted in a specific way, then you should override the locale to ensure things are kept consistent. If you worry about what separators are being use, what currency symbol is used or there the symbol is placed in the string then be sure to set the locale to something specific or make sure that you override all the relevant properties on NSNumberFormatter.

For example, if I know that I want my numbers formatted to the American English locale then I would use the NSLocale(localeIdentifier: "en_US_POSIX") to ensure that it doesn't differ. If you don't mind it being a bit more personal for your users then don't bother specifying a locale.

like image 178
liamnichols Avatar answered Nov 27 '25 04:11

liamnichols


Your problem is that different countries display Euros in a different way. There is no format for "Euro". There is a German currency format, and a French currency format, and an Italian currency format, and so on, and they all display Euros differently.

like image 26
gnasher729 Avatar answered Nov 27 '25 06:11

gnasher729



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!