NumberFormatter
NumberFormatter is a class that provides a flexible and easy way to convert numbers into strings and vice versa.
It is a part of the Foundation framework and is available on all Apple platforms.
In this post, we will explore the various ways in which we can use NumberFormatter to format numbers in Swift.
What Styles does NumberFormatter
have?
Style | en_US Locale | nl_NL Locale | zh_CN Locale |
---|---|---|---|
NumberFormatter.Style.none | 1235 | 1235 | 1235 |
NumberFormatter.Style.decimal | 1,234.568 | 1.234,568 | 1,234.568 |
NumberFormatter.Style.percent | 12% | 12 % | 12% |
NumberFormatter.Style.scientific | 1.2345678E3 | 1,2345678E3 | 1.2345678E3 |
NumberFormatter.Style.spellOut | one hundred twenty-three | honderddrieëntwintig | 一百二十三 |
NumberFormatter.Style.ordinal | 3rd | 3e | 第3 |
NumberFormatter.Style.currency | $1,234.57 | € 1.234,57 | ¥1,234.57 |
NumberFormatter.Style.currencyAccounting | ($1,234.57) | (€ 1.234,57) | (¥1,234.57) |
NumberFormatter.Style.currencyISOCode | USD1,234.57 | EUR 1.234,57 | CNY1,234.57 |
NumberFormatter.Style.currencyPlural | 1,234.57 US dollars | 1.234,57 euro | 1,234.57 人民币 |
Force a specific locale
By default, NumberFormatter
uses the current locale of the device.
You can force a specific locale by setting the locale
property of the NumberFormatter
class.
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "nl_NL")
print(numberFormatter.string(from: 1000)!) // prints "1000" in the Dutch locale
Use Cases
Formatting Numbers
One of the most common use cases for NumberFormatter
is to format numbers.
This can be done using the numberStyle
property of the NumberFormatter
class.
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
print(numberFormatter.string(from: 1000)!) // prints "1,000" in the US locale, "1000" in the Dutch locale
Number to Words (Spell Out)
Another case for NumberFormatter
is to convert a number into a string.
This can be done using the string(from: Number)
function.
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut
print(numberFormatter.string(from: 1990)!) // prints "one thousand nine hundred ninety"
Currency
Another use case for NumberFormatter
is to format numbers as currency. .
This can be done using the currencySymbol
and currencyCode
properties of the NumberFormatter
class.
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.currencyCode = "EUR"
print(numberFormatter.string(from: 10)!) // prints "€10.00"
Conclusion
NumberFormatter is a powerful class that provides a flexible and easy way to convert numbers into strings and vice versa.
It supports a wide range of styles and locales, making it suitable for a variety of use cases.
Whether you need to format numbers, convert numbers to words, or display numbers as currency, NumberFormatter has you covered.
Resources:
Read more
- Mastering animations in SwiftUI • 4 minutes reading time.
- Localizing In Xcode • 3 minutes reading time.
- Why You Should Avoid Using AnyView in SwiftUI • 4 minutes reading time.
Share
Share Bluesky Mastodon Twitter LinkedIn Facebook