Adding Thousand Separator to Int in Swift
You can use NSNumberFormatter to specify a different grouping separator as follow:
update: Xcode 11.5 • Swift 5.2
extension Formatter { static let withSeparator: NumberFormatter = { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.groupingSeparator = " " return formatter }()}
extension Numeric { var formattedWithSeparator: String { Formatter.withSeparator.string(for: self) ?? "" }}
2358000.formattedWithSeparator // "2 358 000"2358000.99.formattedWithSeparator // "2 358 000.99"let int = 2358000let intFormatted = int.formattedWithSeparator // "2 358 000"let decimal: Decimal = 2358000let decimalFormatted = decimal.formattedWithSeparator // "2 358 000"let decimalWithFractionalDigits: Decimal = 2358000.99let decimalWithFractionalDigitsFormatted = decimalWithFractionalDigits.formattedWithSeparator // "2 358 000.99"
If you need to display your value as currency with current locale or with a fixed locale:
extension Formatter { static let number = NumberFormatter()}extension Locale { static let englishUS: Locale = .init(identifier: "en_US") static let frenchFR: Locale = .init(identifier: "fr_FR") static let portugueseBR: Locale = .init(identifier: "pt_BR") // ... and so on}extension Numeric { func formatted(with groupingSeparator: String? = nil, style: NumberFormatter.Style, locale: Locale = .current) -> String { Formatter.number.locale = locale Formatter.number.numberStyle = style if let groupingSeparator = groupingSeparator { Formatter.number.groupingSeparator = groupingSeparator } return Formatter.number.string(for: self) ?? "" } // Localized var currency: String { formatted(style: .currency) } // Fixed locales var currencyUS: String { formatted(style: .currency, locale: .englishUS) } var currencyFR: String { formatted(style: .currency, locale: .frenchFR) } var currencyBR: String { formatted(style: .currency, locale: .portugueseBR) } // ... and so on var calculator: String { formatted(groupingSeparator: " ", style: .decimal) }}
Usage:
1234.99.currency // "$1,234.99"1234.99.currencyUS // "$1,234.99"1234.99.currencyFR // "1 234,99 €"1234.99.currencyBR // "R$ 1.234,99"1234.99.calculator // "1 234.99"
Note: If you would like to have a space with the same width of a period you can use "\u{2008}"
formatter.groupingSeparator = "\u{2008}"
With Swift 5, when you need to format the display of numbers, NumberFormatter
is the right tool.
NumberFormatter
has a property called numberStyle
. numberStyle
can be set to a value of NumberFormatter.Style.decimal
in order to set the formatter's style to decimal.
Therefore, in the simplest case when you want to format a number with decimal style, you can use the following Playground code:
import Foundationlet formatter = NumberFormatter()formatter.numberStyle = NumberFormatter.Style.decimallet amount = 2358000let formattedString = formatter.string(for: amount)print(String(describing: formattedString))
According to the user's current locale, this code will print Optional("2,358,000")
for en_US or Optional("2 358 000")
for fr_FR.
Note that the following code snippet that uses the NumberFormatter
's locale
property set to Locale.current
is equivalent to the previous Playground code:
import Foundationlet formatter = NumberFormatter()formatter.numberStyle = .decimalformatter.locale = Locale.currentlet amount = 2358000let amount = 2358000let formattedString = formatter.string(for: amount)print(String(describing: formattedString))
The Playground code below that uses the NumberFormatter
's groupingSeparator
property set to Locale.current.groupingSeparator
is also equivalent to the former:
import Foundationlet formatter = NumberFormatter()formatter.numberStyle = .decimalformatter.groupingSeparator = Locale.current.groupingSeparatorlet amount = 2358000let formattedString = formatter.string(for: amount)print(String(describing: formattedString))
Otherwise, if you want to set the number formatting with a specific locale formatting style, you may use the following Playground code:
import Foundationlet formatter = NumberFormatter()formatter.numberStyle = .decimalformatter.locale = Locale(identifier: "fr_FR")let amount = 2358000let formattedString = formatter.string(for: amouncomment">// prints: Optional("2 358 000")
However, if what you really want is to enforce a specific grouping separator, you may use the Playground code below:
import Foundationlet formatter = NumberFormatter()formatter.numberStyle = .decimalformatter.groupingSeparator = " "let amount = 2358000let formattedString = formatter.string(for: amount)print(String(describing: formattedString))// prints: Optional("2 358 000")