如何从货币代码获取"locale"?



如何在swift中获得相应货币代码的NSLocale ?

代码:

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 {
   ...
}

你不能;通常有许多地区使用单一货币代码。例如,欧元区的每个国家都共享货币代码"EUR",但它们都有自己的语言环境——有时甚至有几个。类似地,有些货币在系统中没有关联的区域设置。

在大多数情况下,应该始终使用系统区域设置来显示数字。使用特定于所显示货币的本地化显示格式可能会让人困惑,而不是有用的。(例如,它可能会显示一百万美元为"$1,000,000.00",一百万欧元为"€1,000.000,00",一百万卢比为"₹10,00,000.00"。)

您可以从货币代码中获得Locale。

let locale = NSLocale(localeIdentifier: CurrencyCode)

OR使用

获取货币名称
Locale.current.localizedString(forCurrencyCode: CurrencyCode (like "INR"))

最新更新