O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

NumberFormatter

The NumberFormatter class is a subclass of the Formatter abstract class that can be used to convert a number into a human-readable string with a specified format. This formatter is especially useful when we want to display a currency string since it will convert the number to the proper currency for the current locale.

Let's begin by looking at how we would convert a number into a currency string:

let formatter1 = NumberFormatter()  
formatter1.numberStyle = .currency 
let num1 = formatter1.string(from: 23.99) 

In the previous code, we define our number style to be .currency, which tells our formatter that we want to convert our number to a currency string. We then use the string(from:) method to convert the number to a string. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required