How to create currency formatter in iOS

Marmik
2 min readDec 12, 2020

How to create custom currency formatter in iOS and decorate amount as per your requirement.

Do you face challenges while putting currency symbol at right side, customising thousand separator or providing space between currency symbol and amount.

Photo by Jason Leung on Unsplash

So you can achieve any kind of custom output from as per below:

$12,345.00
10 000.00 €
£ -12,345.00

Let’s start!

Problem with Device Locale

So with this approach we need to just provide current device locale to NumberFormatter and use string(from:) function to get the formatted currency text like:

public static func formatAmount(_ amount: Double) -> String {let formatter = NumberFormatter()
formatter.locale = .current
formatter.numberStyle = .currency
formatter.minimumFractionDigits = 2
formatter.negativePrefix = formatter.minusSign
guard let formattedAmount = formatter.string(from: NSNumber(value: amount)) else {
return String(amount)
}
return formattedAmount
}

But problem here with this for Singapore dollar it will return only amount like

let amount = formatAmount(10000)
output: 10,000.00
Expected output: $ 10,000.00

So to provide custom symbol or thousand separator or putting symbol to right side you can use below function where we provide currencySymbol as empty string and grouping separator set as per requirement for specific currency. After that used formatter.string function which provides formatted text without symbol with separator and fraction digits as provided. Now after that it checks for symbol left / right position and whether spacing require between symbol and amount or not.

public struct Currency {public let symbol: String
public let thousandsSeparator: String
public let symbolOnLeft: Bool
public let spaceBetweenAmountAndSymbol: Bool
}
public static func formatAmount(withCurrency currency: Currency, andAmount amount: Double) -> String {let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.minimumFractionDigits = 2
formatter.currencySymbol = ""
formatter.currencyGroupingSeparator = currency.thousandsSeparator
formatter.negativePrefix = formatter.minusSign
guard var formattedAmount = formatter.string(from: NSNumber(value: amount)) else {
return String(amount)
}
if currency.symbolOnLeft {
if currency.spaceBetweenAmountAndSymbol {
formattedAmount = "\(currency.symbol) \(formattedAmount)"
}
else {
formattedAmount = "\(currency.symbol)\(formattedAmount)"
}
}
else {
if currency.spaceBetweenAmountAndSymbol {
formattedAmount = "\(formattedAmount) \(currency.symbol)"
}
else {
formattedAmount = "\(formattedAmount)\(currency.symbol)"
}
}
return formattedAmount
}

With this create your model for currency as you need and customise it property as require.

let dollarCurrency = Currency(symbol: "$",
thousandsSeparator: ",",
symbolOnLeft: true,
spaceBetweenAmountAndSymbol: false)
let euroCurrency = Currency(symbol: "€",
thousandsSeparator: " ",
symbolOnLeft: false,
spaceBetweenAmountAndSymbol: true)
let poundCurrency = Currency(symbol: "£",
thousandsSeparator: ",",
symbolOnLeft: true,
spaceBetweenAmountAndSymbol: true)
let dollarCurrencyText = formatAmount(withCurrency: dollarCurrency, andAmount: 12345)let euroCurrencyText = formatAmount(withCurrency: euroCurrency, andAmount: 10000)let poundCurrencyText = formatAmount(withCurrency: poundCurrency, andAmount: -12345)Output:
$12,345.00
10 000.00 €
£ -12,345.00

--

--