Fast Currency Format Standard ISO Conversion Problems

After reading Apple docs for NSNumberFormatter here I am trying to convert currency according to Uber API docs. Both documents state that they use the ISO formatting standard, however in my code I believe this is not the case. ISO 4217 standard here  and at ISO

public static func convertStringCurrencyToNumber(strCurrency:String, locale:String)->Double {
    var formatter = NSNumberFormatter()
    formatter.locale = NSLocale(localeIdentifier: "\(locale)")
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    println(formatter.numberFromString(strCurrency))
    if let converted:Double = formatter.numberFromString(strCurrency)?.doubleValue {
        return converted
    }else{
        return 0.0
    }

}

      

My Unit Test for this function

func testCurrencyConversion() {
    let fiveBucks = UberReceipt.convertStringCurrencyToNumber("$5.00", locale: "en_US")
    println(fiveBucks)
    let tenBucks = UberReceipt.converStringCurrencyToNumber("$10.00", locale:"USD")
    println(tenBucks)
}

      

Console Log: Optional (5.0) zero 5.0 0.0

If I use "en_US" the result is the same expected as 5.0, however if I use what Uber returns as the locale "USD" the return value is 0.0. After printing / checking what happens when the formatter converts the currency, I find it returns zero.

I find the documentation is misleading as to what it says about formatter.locale = NSLocale (...). According to the docs, "Locale defines default values ​​for many formatting attributes such as country and ISO language codes, currency code ...."

I was convinced that setting the language should also set the currency code. This is not true? Why then does my code work if I use the non-standard code "en_US" as stated in the ISO code (which Apple Documentation supposedly uses) "USD"

+3


source to share


2 answers


public static func convertStringCurrencyToNumber(strCurrency:String, locale:String)->Double {
    var formatter = NSNumberFormatter()
    formatter.currencyCode = locale
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    if let converted:Double = formatter.numberFromString(strCurrency)?.doubleValue {
        return converted
    }else{
        return 0.0
    }
}

      



Changing formatter.locale to formatter.currencyCode works as expected with ISO 4127 "USD" country and currency code.

+5


source


As Zoff Dino said, USD is not a valid locale identifier. To get a complete list, you can run:

NSLocale.availableLocaleIdentifiers() 

      

You are correct that the locale sets the currency code. Try for example:



let l = NSLocale(localeIdentifier: "de_CH")
let f = NSNumberFormatter()
f.locale = l
f.currencyCode // Playground shows CHF

      

The locale defines the correct alphabetical order, thousands separator, time and date format, currency code, and other things. What you are going through is not a locale but a currency code. If you want to generate a language code from a currency code, you can create a lookup table from the output NSLocale.availableLocaleIdentifiers()

and resulting currency codes.

It can help if you clarify what exactly the API is returning and what exactly you want to do with it.

+1


source







All Articles