Get currency symbols from currency code with swift

前端 未结 11 673
星月不相逢
星月不相逢 2020-12-24 12:21

How can I get the currency symbols for the corresponding currency code with Swift (macOS).

Example:

  • EUR = €1.00
  • USD = $1.00<
11条回答
  •  半阙折子戏
    2020-12-24 12:53

    Swift 4 Version of Pancho's answer, As the String.characters is deprecated now.

    We can simply apply dropLast() on String.

    func getCurrencySymbol(from currencyCode: String) -> String? {
    
        let locale = NSLocale(localeIdentifier: currencyCode)
        if locale.displayName(forKey: .currencySymbol, value: currencyCode) == currencyCode {
            let newlocale = NSLocale(localeIdentifier: currencyCode.dropLast() + "_en")
            return newlocale.displayName(forKey: .currencySymbol, value: currencyCode)
        }
        return locale.displayName(forKey: .currencySymbol, value: currencyCode)
    }
    

提交回复
热议问题