How to format a Double into Currency - Swift 3
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To format a Double as currency in Swift, use NumberFormatter with numberStyle = .currency. It automatically applies the correct currency symbol, decimal separator, grouping separator, and decimal places based on the user's locale. For Swift 5.5+ (iOS 15+), the formatted(.currency(code:)) API is even simpler. Avoid manual string interpolation like "$\(String(format: "%.2f", amount))" — it ignores locale, breaks for non-USD currencies, and uses the wrong separators for many regions.
NumberFormatter (All Swift Versions)
NumberFormatter uses the device's current locale by default. It handles currency symbols, grouping separators, decimal separators, and the number of fraction digits automatically.
Specifying a Locale
Specifying a Currency Code
To show a specific currency regardless of locale:
Formatted API (iOS 15+ / Swift 5.5+)
Reusable Extension
Customizing Decimal Places
Using in SwiftUI
Common Pitfalls
- Using string interpolation instead of NumberFormatter:
"$\(String(format: "%.2f", amount))"hardcodes the dollar sign and uses.as the decimal separator. This is incorrect for most non-US locales where the comma is the decimal separator and the currency symbol differs or appears after the number. - Creating a new NumberFormatter on every call:
NumberFormatteris expensive to initialize. In table views or collection views, create the formatter once and reuse it. Store it as a property or use a static instance. - Forgetting that
Doublehas floating-point precision issues:0.1 + 0.2is not exactly0.3in floating point. For financial calculations, useDecimalorNSDecimalNumberinstead ofDoubleto avoid rounding errors that appear in formatted output. - Hardcoding a locale instead of using
.current: HardcodingLocale(identifier: "en_US")forces US formatting for all users. UseLocale.currentunless you specifically need a fixed locale (like displaying prices in a specific currency for all users). - Not handling the optional return from
formatter.string(from:):NumberFormatter.string(from:)returns an optionalString?. If the value isNaNorinfinity, it returnsnil. Always provide a fallback with?? "$0.00"or use a guard statement.
Summary
- Use
NumberFormatterwith.currencystyle for locale-aware currency formatting - Use
.formatted(.currency(code: "USD"))on iOS 15+ for a concise one-liner - Set
currencyCodeto format a specific currency regardless of locale - Create a reusable
Doubleextension to avoid duplicating formatter setup - Use
Decimalinstead ofDoublefor financial calculations to avoid floating-point rounding errors
Related reading
- How to format date and time in Android?
- How to format DateTime in Flutter
- How to format DateTime in Flutter
- How to generate a random number in Swift?
- How to generate buildConfigField with String type
- How to generate UUID in ios
- How to get a context in a recycler view adapter
- How to get a context in a recycler view adapter
.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 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.