custom number formatter with currency symbol

1.8k Views Asked by At

I have a text field where the user should be enter a price. On this text field I dragged a Custom Number Formatter with this settings:

enter image description here

This works nearly perfect but I have a problem with the "Format". At the moment it is: .#.##

But what I have to change to show automatically the correct user currency symbol at the end?

3

There are 3 best solutions below

8
On BEST ANSWER

For the static solution you can simply change format from .#.## to .#.## €

If you're looking for universal way to do it, you're interested with symbol ¤, which is responsible for currency sign.

So the format which are you looking for is ,#.## ¤

Last step is to check option lenient, otherwise you need to provide full format in text field.

More details

Since OSX 10.9 and iOS 7 the format strings uses patterns from the Unicode Technical Standard #35 of version 31.

You can find all number format patterns in this version here

Apple documentation about number formatters

0
On

Can you use the default currency mode provided by the formatter or do you need to do additional custom formatting:

enter image description here

2
On

You can use the number formatter also like this:

let price = 592.12
let formatter = NumberFormatter()
formatter.numberStyle = .currency

//change this to get your desired currency
formatter.locale = Locale(identifier: "de_DE")
formatter.maximumFractionDigits = 0;

let priceText = formatter.string(for: price)
//priceText = 592,12 €