Typically, when you go to display a number in Swift (Float, Double, Int, etc.) it will display without grouping separators. By default a number like 4,592,028.0 will display like:
If you need to display currency, you will want to show the currency symbol ($, €, ¥, £, etc.).
Displaying the correct currency symbol can get complex pretty quickly – thankfully, Apple has you covered and provides a solution with the
NSNumberFormatter class. It can take care of displaying the correct symbol based on your user’s location in addition to any rules for decimal separators and grouping separators.
- In the USA: $3,490,000.89
- In France: 3 490 000,89 €
- In Germany: 3.490.000,89 €
You don't have to memorize these currency symbols, grouping separators, or decimal separators. Apple has done all the heavy lifting with
The following code sample will use the current locale of the user's device to format the currency:
var currencyFormatter = NSNumberFormatter() currencyFormatter.usesGroupingSeparator = true currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle // localize to your grouping and decimal separator currencyFormatter.locale = NSLocale.currentLocale() var priceString = currencyFormatter.stringFromNumber(9999.99) print(priceString) // Displays $9,999.99 in the US locale
NSNumberFormatter can use the correct separators and currency symbols for your country by setting the
Try changing the
currencyGroupingSeparator, and related properties to format both decimal style numbers and currency style numbers.
Read the documentation for
NSNumberFormatter and experiment with changing how it formats your decimal numbers (i.e. try different grouping separators like:
Download the Swift Playgrounds for NSNumberFormatter
Play with the
NSNumberFormatter Playgrounds file.
Download the currency Playgrounds file and you can learn how to programmatically set the NSLocale to the German, French, and US English.