Reputation: 713
How can I get NSLocale for the corresponding currency code in swift?
The code:
func format(amount: String, code: String) -> NSDictionary {
var formatter = NSNumberFormatter()
let locale = getLocaleByCurrencyCode(code)
formatter.locale = locale
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let number = NSNumber(double: (amount as NSString).doubleValue)
return ["locale":locale, "amount": formatter.stringFromNumber(number)!]
}
func getLocaleByCurrencyCode(code) -> NSLocale {
...
}
Upvotes: 3
Views: 5672
Reputation: 932
You can get Locale from the currency code.
let locale = NSLocale(localeIdentifier: CurrencyCode)
OR Get Currency name using
Locale.current.localizedString(forCurrencyCode: CurrencyCode (like "INR"))
Upvotes: -1
Reputation:
You can't; there are often many locales with a single currency code. For instance, every country in the Eurozone shares the currency code "EUR", but they all have their own locale - sometimes even several. Similarly, there are some currencies which do not have an associated locale in the system.
In most situations, you should always use the system locale to display numbers. Using a localized display format specific to the currency being displayed is likely to be more confusing than useful. (For instance, it's liable to display a million dollars as "$1,000,000.00", a million euros as "€1.000.000,00", and a million rupees as "₹10,00,000.00".)
Upvotes: 6