Get currency symbols from currency code with swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To get a currency symbol (like $, €, £) from a currency code (like USD, EUR, GBP) in Swift, use Locale and NumberFormatter from the Foundation framework. The approach is to find a locale that uses the given currency code, then extract its currencySymbol. Since one currency code can map to multiple locales (e.g., USD is used in en_US, es_US, and others), you typically pick the first matching locale or a specific one. NumberFormatter can also format amounts with the correct symbol and placement for any locale.
Basic Currency Symbol Lookup
Optimized with Dictionary Cache
Using NumberFormatter
NumberFormatter formats amounts with the correct symbol, placement, and decimal separator:
Getting Just the Symbol from NumberFormatter
Locale-Aware Symbol Display
The same currency code produces different symbols depending on the locale:
Note how en_GB uses US$ to disambiguate from £, and fr_FR uses $US with different number formatting.
Common Currency Codes and Symbols
| Code | Symbol | Currency |
| USD | ||
| EUR | € | Euro |
| GBP | £ | British Pound |
| JPY | ¥ | Japanese Yen |
| CNY | ¥ | Chinese Yuan |
| KRW | ₩ | South Korean Won |
| INR | ₹ | Indian Rupee |
| BRL | ||
| RUB | ₽ | Russian Ruble |
| THB | ฿ | Thai Baht |
SwiftUI Integration
Handling Edge Cases
Common Pitfalls
- Iterating all locales on every lookup:
Locale.availableIdentifierscontains hundreds of entries. Iterating it for every symbol lookup is slow. Build a dictionary cache once (as shown inCurrencyHelper) and reuse it throughout the app. - Assuming
$always means USD: Many currencies use$(USD, CAD, AUD, NZD, SGD). In non-US locales,$may refer to the local dollar. Use the full currency code alongside the symbol for clarity, or letNumberFormatterhandle disambiguation. - Hardcoding decimal places for currencies: JPY uses 0 decimal places, KWD uses 3, and most others use 2.
NumberFormatterwith.currencystyle handles this automatically. Never format currency amounts with a fixed 2-decimal format. - Ignoring locale differences in symbol placement: Some locales place the symbol before the amount (
$100), others after (100 €), and some use different decimal/grouping separators. Always useNumberFormatterfor display rather than string concatenation like"$" + amount. - Using
Locale.currentin unit tests:Locale.currentdepends on the device/simulator settings and changes between machines. PassLocaleas a parameter in functions you test, and use a fixed locale likeLocale(identifier: "en_US")in tests for consistent results.
Summary
- Use
Locale.availableIdentifiersto find a locale matching a currency code, then accesscurrencySymbol - Cache the code-to-symbol mapping in a dictionary for performance
- Use
NumberFormatterwith.currencystyle for properly formatted amounts with correct symbols, decimal places, and placement - The same currency code renders differently depending on locale — always use
NumberFormatterfor user-facing display - Build a
CurrencyHelpersingleton to provide fast symbol lookups across your app
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.