Understanding NumberFormatter for currency in Swift

swift numberformatter currency
swift numberformatter decimal
swift format number with commas
swift numberformatter thousand separator
nsnumberformatter currency
swift numberformatter decimal places
swift currency
swiftui numberformatter

I have an app where I'm trying to format a Double with NumberFormetter but the output I'm getting does not match the output in other apps.

Here is the code I'm using...

let price:Double = 25
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = NSLocale.current

let priceFormatter = currencyFormatter.string(for: price)

myLabel.text = priceFormatter

With the above code and with the phone's Regionset to China in my App I get 25,00 CNY but in two other third-party-apps the output is CN¥25.00.

What am I doing wrong?

Region Set to China:

My App 25,00 CNY

Other Apps CN¥25.00

Region Set to France:

My App 25,00€

Other Apps €25,00

But when the region is set to United States I get the same notation in all three (3) apps.

Region Set to United States:

My App $25.00

Other Apps $25.00

I'm a little concerned especially with the output when the region is set to China because besides the notation being different I also get a different symbol CNY vs CN¥.

Any suggestions, am I doing something wrong?

Spoiler the other apps are doing it wrong, your output is correct !

How to Use NumberFormatter (NSNumberFormatter) in Swift to , NumberFormatter.Style.currency. A currency style format that uses the currency symbol defined by the number formatter locale. Availability. iOS 2.0+; macOS� let price:Double = 25 let currencyFormatter = NumberFormatter() currencyFormatter.usesGroupingSeparator = true currencyFormatter.numberStyle = .currency currencyFormatter.locale = NSLocale.current let priceFormatter = currencyFormatter.string(for: price) myLabel.text = priceFormatter

You can specify the format you want the output. Exemple :

let number: NSNumber = 25
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale.init(identifier: "cn_CN") // or "en_US", "fr_FR"
numberFormatter.positiveFormat = "#0.00 ¤"
let price = numberFormatter.string(from: number)

which gives the output

25.00 CN¥

You can change the format to get different outputs:

"#0.00 ¤" => 25.00 CN¥

"#0.00 ¤¤" => 25.00 CNY

"#0.00 ¤¤¤" => 25.00 Chinese yuan

NumberFormatter.Style.currency, Spoiler the other apps are doing it wrong, your output is correct ! NumberFormatter+Currency.swift. GitHub Gist: instantly share code, notes, and snippets.

It simply depends entirely what you want to do. In short, when presenting results in the user interface, it is a function of the locale of the device, not the currency in question.

For example, if I was displaying ten thousand US dollars exchanged into in euros at today’s exchange rate, you might display 8.950,95 € on a device with a German locale. Both the number formatting and placement of the currency code would be localized for the device in question, not the currency.

However, if I was showing the same result on a US device, I might present it with customary US number/currency formatting, e.g. perhaps €8,950.95.

FWIW, English-language style guides, such as The Chicago Manual of Style and The Economist Style Guide, confirm this approach. I assume the "preserve the audience’s expectation for foreign currencies" applies in other languages, too.

Bottom line, one should generally format numbers in the user interface in accordance with the device locale, but obviously employ the currency symbol and number of fractional digits in accordance with the currency being represented.

Understanding NumberFormatter for currency in Swift, Here's an example on how to use it on Swift 3. ( Edit: Works in Swift 4 too ) let price = 123.436 as NSNumber let formatter = NumberFormatter() formatter. The NumberFormatter class provides flexible options for displaying non-zero fractional parts of numbers. If you set the usesSignificantDigits property to true, you can configure NumberFormatter to display significant digits using the minimumSignificantDigits and maximumSignificantDigits properties.

Struggling with NSNumberFormatter in Swift for currency, Swift Objective-C. let number = 1234.5678 // 🤔 let formatter = NumberFormatter() formatter.numberStyle = .currency let 🇺🇸 = Locale(identifier:� SWIFT is a vast messaging network used by banks and other financial institutions to quickly, accurately, and securely send and receive information, such as money transfer instructions.

Formatter, How to convert UITextField input to display currency and how to convert back to double format for calculation. return numberFormatter.string(from: NSNumber( value: amount))! } So what is happening inside the functions? Let’s take a look at a quick example that will help us to understand how it works. extension NumberFormatter { static var currency : NumberFormatter { let formatter = NumberFormatter () formatter . numberStyle = . currency return formatter } } struct ContentView : View { @State private var price = 99 var body : some View { TextField ( "type something" , value : $ price , formatter : NumberFormatter . currency ) } }

Currency converter in Swift 4.2. How to convert UITextField input to , Best swift Tutorials, Articles, Tips and Tricks by Yogesh Chauhan. Well written, nicely organized, simple to learn and easy to understand Web� Benefitting from SWIFT’s central role within the financial industry, SWIFT Innotribe seeks to reinforce the importance of collaboration when it comes to innovation, supporting all key players in the fintech ecosystem, including SWIFT, to move forward together.