How to Use NSNumberFormatter in Swift to Make Currency Numbers Easy to Read

NSNumberFormatter Currency Price String in Swift

Typically, when you go to display a number in Swift (Float, Double, Int, etc.) it will display without grouping separators. By default a number like 4,592,028.0 will display like: 4592028.0

If you need to display currency, you will want to show the currency symbol ($, €, ¥, £, etc.).

Displaying the correct currency symbol can get complex pretty quickly – thankfully, Apple has you covered and provides a solution with the NSNumberFormatter class. It can take care of displaying the correct symbol based on your user’s location in addition to any rules for decimal separators and grouping separators.

  • In the USA: $3,490,000.89
  • In France: 3 490 000,89 €
  • In Germany: 3.490.000,89 €

You don't have to memorize these currency symbols, grouping separators, or decimal separators. Apple has done all the heavy lifting with NSNumberFormatter.

The following code sample will use the current locale of the user's device to format the currency:

var currencyFormatter = NSNumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle 
// localize to your grouping and decimal separator
currencyFormatter.locale = NSLocale.currentLocale()
var priceString = currencyFormatter.stringFromNumber(9999.99)
print(priceString) // Displays $9,999.99 in the US locale

NSNumberFormatter can use the correct separators and currency symbols for your country by setting the NSLocale directly.

Homework

Try changing the NSNumberFormatter's groupingSeparator, currencyGroupingSeparator, and related properties to format both decimal style numbers and currency style numbers.

Read the documentation for NSNumberFormatter and experiment with changing how it formats your decimal numbers (i.e. try different grouping separators like: "_" or "*").

Download the Swift Playgrounds for NSNumberFormatter

Play with the NSNumberFormatter Playgrounds file.

Download the currency Playgrounds file and you can learn how to programmatically set the NSLocale to the German, French, and US English.

Logitech c930 vs Logitech c920 web camera review - Amazing web cameras for online teaching on Teachable

If you teach online courses and want a camera with an incredible focus distance for detailed work, you need to get the c930 from Logitech.

Focal distance is insane compared to my Canon 6D DSLR (30x the price!) with the c930. And it auto focuses super fast.

It's a super wide angle lens and it gives you more creative freedom to get close to your work with a configurable mini-tripod mount from Joby.

Watch the c930 in action

Logitech c930 super close auto focus and wide angle lens.

Both cameras are 1080p and look stellar with auto focus and can focus within ~2 cm. Super FUN!

And if you teach online, make sure you use Teachable.com to host all your videos + payment with Paypal + Credit Cards!  

It's so easy, so much better than Skillshare or Udemy!

Bug alert: sometimes I've noticed the c930 will bug out and skip frames... close the app using it and reopen the app on Mac and it seems to get normal again

Live iPhone App Workshop on Thursday and 24/7 Live Chat

If you want to make an app but don’t know how to get started, tomorrow is the day for you to fill in that blank.

I will be providing a free iPhone app workshop that will show you everything you need to know to get started with Xcode and Swift . . . even if you don’t currently know what either of those words mean.

If you don’t have any coding experience . . .

If you don’t have any user interface design experience . . . 

If you think that apps are basically magic and coding can only be done by people with computer science degrees . . . 

. . .  tomorrow you will learn how to get started (the easy way!).

I LOVE working with people who have ideas but who don’t know how to get started.

It’s so INSPIRING to see real people (my students!) develop and publish apps that help populations around the world deal with things like power outages, healthy lifestyle choices, living with various disabilities, and teaching children how to read and speak . . . just to name a few.

Cute Zoo ABC by Sasha

Cute Zoo ABC by Sasha

I know technology can be intimidating—it changes frequently, leaving the uninitiated in the dust on an almost daily basis.

To bridge that gap, provide a solid foundation, and get you moving forward in your career change, Super Easy iPhone Apps is the place to start. Whether you want to switch careers or publish the next million dollar app, it’s an investment in your future.

You need to start like everyone else: learn enough technical skills to create a prototype and work with a team to execute your vision.

I’ve already helped thousands of people imagine and develop their first iPhone apps:

  • Like Bennie, who started an app to help with power outages in South Africa;
  • Or Rajee, who switched jobs from QA to iOS Developer: “I have great news...I GOT THE JOB!”
  • Or Sasha, who made Cute Zoo ABC for his 18 month old son . . . 

I hope that I can add your name to the list of people I have helped.

 I’m doing a FREE training on Thursday about making your first iPhone app (even if you are starting from zero).

I know it will be worth your time.

Register for Thursday November 19th at 2pm eastern.

This workshop is totally free—but the information that I will be sharing is potentially worth thousands of dollars if you come live, take notes, and commit to taking action.

If you are just starting to imagine app ideas, getting ready to publish your own iPhone app, or just want to up your game on your next app idea, you will walk away with actionable strategies you can use immediately.

It is really important to me that you make this small time investment in yourself and your career. It costs nothing other than an hour of your time—and could really change everything.

Register for Thursday November 19th at 2pm eastern.

I’ll see you there, live!

PS If you want to join my upcoming 24/7 live chat for people building iPhone apps, you won't want to miss this workshop.