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. ...