I want to display Currency Symbol based on Country name or country code using NSLocale I have all the country name list. suppose I have selected USA then it Return $ Currency
Code :
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCurrencyCode value: countryCode];
You can build your own list of country codes and that country's currency symbol using the following code:
Objective-C:
- (void)listCountriesAndCurrencies {
NSArray<NSString *> *localeIds = [NSLocale availableLocaleIdentifiers];
NSMutableDictionary<NSString *, NSString *> *countryCurrency = [NSMutableDictionary dictionary];
for (NSString *localeId in localeIds) {
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:localeId];
NSString *country = [locale objectForKey: NSLocaleCountryCode];
if (country && country.length == 2) {
NSString *currency = [locale objectForKey: NSLocaleCurrencySymbol];
countryCurrency[country] = currency;
}
}
NSArray<NSString *> *sorted = [countryCurrency.allKeys sortedArrayUsingSelector:@selector(compare:)];
for (NSString *country in sorted) {
NSString *currency = countryCurrency[country];
NSLog(@"country: %@, currency: %@", country, currency);
}
}
This code starts with the list of all locale ids. From there it creates each associated NSLocale
. For each locale, the country code and currency symbol is extracted. These are used to build a dictionary of country codes and currency symbols which are then printed out.
Here's the same code translated to Swift 3:
func listCountriesAndCurrencies() {
let localeIds = Locale.availableIdentifiers
var countryCurrency = [String: String]()
for localeId in localeIds {
let locale = Locale(identifier: localeId)
if let country = locale.regionCode, country.characters.count == 2 {
if let currency = locale.currencySymbol {
countryCurrency[country] = currency
}
}
}
let sorted = countryCurrency.keys.sorted()
for country in sorted {
let currency = countryCurrency[country]!
print("country: \(country), currency: \(currency)")
}
}
This results in the following list when run on a simulated iOS 10 device:
country: AD, currency: € country: AE, currency: د.إ. country: AF, currency: ؋ country: AG, currency: $ country: AI, currency: $ country: AL, currency: Lekë country: AM, currency: ֏ country: AO, currency: Kz country: AR, currency: $ country: AS, currency: $ country: AT, currency: € country: AU, currency: $ country: AW, currency: Afl. country: AX, currency: € country: AZ, currency: ₼ country: BA, currency: KM country: BB, currency: $ country: BD, currency: ৳ country: BE, currency: € country: BF, currency: CFA country: BG, currency: лв. country: BH, currency: د.ب. country: BI, currency: FBu country: BJ, currency: CFA country: BL, currency: € country: BM, currency: $ country: BN, currency: $ country: BO, currency: Bs country: BQ, currency: $ country: BR, currency: R$ country: BS, currency: $ country: BT, currency: Nu. country: BW, currency: P country: BY, currency: р. country: BZ, currency: $ country: CA, currency: $ country: CC, currency: $ country: CD, currency: FC country: CF, currency: FCFA country: CG, currency: FCFA country: CH, currency: CHF country: CI, currency: CFA country: CK, currency: $ country: CL, currency: $ country: CM, currency: FCFA country: CN, currency: ¥ country: CO, currency: $ country: CR, currency: ₡ country: CU, currency: $ country: CV, currency: country: CW, currency: NAf. country: CX, currency: $ country: CY, currency: € country: CZ, currency: Kč country: DE, currency: € country: DG, currency: US$ country: DJ, currency: Fdj country: DK, currency: kr. country: DM, currency: $ country: DO, currency: RD$ country: DZ, currency: د.ج. country: EA, currency: € country: EC, currency: $ country: EE, currency: € country: EG, currency: ج.م. country: EH, currency: د.م. country: ER, currency: Nfk country: ES, currency: € country: ET, currency: Br country: FI, currency: € country: FJ, currency: $ country: FK, currency: £ country: FM, currency: US$ country: FO, currency: kr country: FR, currency: € country: GA, currency: FCFA country: GB, currency: £ country: GD, currency: $ country: GE, currency: ₾ country: GF, currency: € country: GG, currency: £ country: GH, currency: GH₵ country: GI, currency: £ country: GL, currency: kr. country: GM, currency: D country: GN, currency: FG country: GP, currency: € country: GQ, currency: FCFA country: GR, currency: € country: GT, currency: Q country: GU, currency: $ country: GW, currency: CFA country: GY, currency: $ country: HK, currency: HK$ country: HN, currency: L country: HR, currency: HRK country: HT, currency: G country: HU, currency: HUF country: IC, currency: € country: ID, currency: Rp country: IE, currency: € country: IL, currency: ₪ country: IM, currency: £ country: IN, currency: ₹ country: IO, currency: US$ country: IQ, currency: IQD country: IR, currency: IRR country: IS, currency: ISK country: IT, currency: € country: JE, currency: £ country: JM, currency: $ country: JO, currency: د.أ. country: JP, currency: ¥ country: KE, currency: Ksh country: KG, currency: сом country: KH, currency: ៛ country: KI, currency: $ country: KM, currency: CF country: KN, currency: $ country: KP, currency: KPW country: KR, currency: ₩ country: KW, currency: د.ك. country: KY, currency: $ country: KZ, currency: ₸ country: LA, currency: ₭ country: LB, currency: ل.ل. country: LC, currency: $ country: LI, currency: CHF country: LK, currency: Rs. country: LR, currency: $ country: LS, currency: R country: LT, currency: € country: LU, currency: € country: LV, currency: € country: LY, currency: د.ل. country: MA, currency: MAD country: MC, currency: € country: MD, currency: L country: ME, currency: € country: MF, currency: € country: MG, currency: Ar country: MH, currency: $ country: MK, currency: den country: ML, currency: CFA country: MM, currency: K country: MN, currency: ₮ country: MO, currency: MOP$ country: MP, currency: $ country: MQ, currency: € country: MR, currency: UM country: MS, currency: $ country: MT, currency: € country: MU, currency: Rs country: MV, currency: MVR country: MW, currency: MK country: MX, currency: $ country: MY, currency: RM country: MZ, currency: MTn country: NA, currency: $ country: NC, currency: FCFP country: NE, currency: CFA country: NF, currency: $ country: NG, currency: ₦ country: NI, currency: C$ country: NL, currency: € country: NO, currency: kr country: NP, currency: नेरू country: NR, currency: $ country: NU, currency: $ country: NZ, currency: $ country: OM, currency: ر.ع. country: PA, currency: B/. country: PE, currency: S/. country: PF, currency: FCFP country: PG, currency: K country: PH, currency: ₱ country: PK, currency: Rs country: PL, currency: PLN country: PM, currency: € country: PN, currency: $ country: PR, currency: $ country: PS, currency: ₪ country: PT, currency: € country: PW, currency: US$ country: PY, currency: Gs. country: QA, currency: ر.ق. country: RE, currency: € country: RO, currency: RON country: RS, currency: RSD country: RU, currency: RUB country: RW, currency: RF country: SA, currency: ر.س. country: SB, currency: $ country: SC, currency: SR country: SD, currency: SDG country: SE, currency: kr country: SG, currency: $ country: SH, currency: £ country: SI, currency: € country: SJ, currency: kr country: SK, currency: € country: SL, currency: Le country: SM, currency: € country: SN, currency: CFA country: SO, currency: S country: SR, currency: $ country: SS, currency: £ country: ST, currency: Db country: SV, currency: $ country: SX, currency: NAf. country: SY, currency: ل.س. country: SZ, currency: E country: TC, currency: US$ country: TD, currency: FCFA country: TG, currency: CFA country: TH, currency: THB country: TJ, currency: сом country: TK, currency: $ country: TL, currency: US$ country: TM, currency: TMT country: TN, currency: د.ت. country: TO, currency: T$ country: TR, currency: TRY country: TT, currency: $ country: TV, currency: $ country: TW, currency: NT$ country: TZ, currency: TSh country: UA, currency: ₴ country: UG, currency: USh country: UM, currency: $ country: US, currency: $ country: UY, currency: $ country: UZ, currency: soʻm country: VC, currency: $ country: VE, currency: Bs. country: VG, currency: US$ country: VI, currency: $ country: VN, currency: ₫ country: VU, currency: VT country: WF, currency: FCFP country: WS, currency: WS$ country: XK, currency: € country: YE, currency: ر.ي. country: YT, currency: € country: ZA, currency: R country: ZM, currency: K country: ZW, currency: US$
Xcode 10 • Swift 4.2 or later
extension Locale {
static let currency: [String: (code: String?, symbol: String?)] = Locale.isoRegionCodes.reduce(into: [:]) {
let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: $1]))
$0[$1] = (locale.currencyCode, locale.currencySymbol)
}
}
Locale.currency["US"] // (code "USD", symbol "US$")
Locale.currency["BR"] // (code "BRL", symbol "R$")
Locale.currency["GB"] // (code "GBP", symbol "£")
Locale.currency["PT"] // (code "EUR", symbol "€")
For older Swift syntax please check the post edit history
You can Get currency code from Country name, tested it on swift3, first of all add this extension
extension NSLocale {
class func locales1(countryName1 : String) -> String {
let locales : String = ""
for localeCode in NSLocale.isoCountryCodes {
let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode)
if countryName1.lowercased() == countryName?.lowercased() {
return localeCode
}
}
return locales
}
}
You will get the currency code from here
let countryCode = NSLocale.locales1(countryName1: "\(place.name)")
let countryCodeCA = countryCode
let localeIdCA = NSLocale.localeIdentifier(fromComponents: [ NSLocale.Key.countryCode.rawValue : countryCodeCA])
let localeCA = NSLocale(localeIdentifier: localeIdCA)
let currencySymbolCA = localeCA.object(forKey: NSLocale.Key.currencySymbol)
let currencyCodeCA = localeCA.object(forKey: NSLocale.Key.currencyCode)
print("\(currencyCodeCA!)")
self.currencyKey = currencyCodeCA! as! String
You can't do that with the currency symbol alone. You have to let the system format your numbers as a currency with the correct locale.
For example, there are over a dozen countries using Euros, and they display money in different ways. And the currency symbol for the USA is only "$" if you are in the USA - in Canada, for example, it is "US$" because Canadians use "$" for their own currency.
来源:https://stackoverflow.com/questions/40270754/get-currency-symbol-based-on-country-code-or-country-name-using-nslocale