Reputation: 9466
I have a currency format func that looks like this:
func currencyFormat() -> NumberFormatter{
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = NumberFormatter.Style.currency
// localize to your grouping and decimal separator
currencyFormatter.locale = NSLocale.current
return currencyFormatter
}
So when I use it like this:
currencyFormat().string(for: 20.34) --> $20.34
Same goes if I send over a negative value
currencyFormat().string(for: -20.34) --> -$20.34
I don't want the negative sign to come back formatted with the currency
It works great but the issue I'm having is if I send it a negative decimal number, I get back the negative sign. Is there a way to drop that sign? Should I convert the decimal to positive before I send it over to the converter?
Upvotes: 2
Views: 697
Reputation: 3945
Ensure the number is positive before sending it through the currency formatter by taking the absolute value:
let number = -500
let positiveNumber = abs(number)
let formattedNumber = currencyFormat().string(for: positiveNumber)
This is the simplest and most readable way to do it.
Upvotes: 1