Chances are good you’ll need to manipulate numbers. However, when you want to display those numbers as a text string to display in a label or text field, you’ll need to convert that number into a string.
The simplest way to convert a number into a string is to use the \( ) method inside quotation marks where you put the number inside the parentheses such as:
In the above example, the “stringHere” variable holds the text string “This is a string = 2.3094”. While this is fine, another method is to actively convert the number into a string by using String( ) where the number goes inside the parentheses such as:
This code works exactly like the previous code so the “stringHere” variable holds the text string “This is a string = 2.3094”. Either method is fine, but there’s a third way to convert numbers into strings and that’s to use the NumberFormatter class.
Some advantages of the NumberFormatter class include:
- Letting you define how many decimal places to display
- Letting you define whether to round up or down
- Letting you work with different number formats such as currency
To use the NumberFormatter class, you need to create an object such as:
Now you can specify the maximum number of decimal places you want to include. If you wanted to display two decimal places, you could use the following:
Once you’ve defined the maximum number of decimal places to display, you can also choose a rounding mode such as one of the following:
- down – always rounds down
- up – always rounds up
When rounding down to two decimal places, a number like 2.3094 would turn into 2.30.
When rounding up to two decimal places, a number like 2.3094 would turn into 2.31.
To define how to round numbers, you could use this code:
To retrieve a string from a number, you must use a number of the NSNumber data type such as:
Now you’ll be able to convert a number into a string using this command:
So the whole code would look like this:
Copy and paste this code into a Swift playground to see how the NumberFormatter class works to convert a number into a string. While using the NumberFormatter class might seem clumsier, it does give you more control over the decimal places to display and how to round a number.
What makes the NumberFormatter class even more useful is that it can convert numbers into one of the following:
- currency – displays the local currency symbol with the number
- ordinal – displays numbers like 2nd or 8th
- percent – displays numbers with a % sign at the end
- spellOut – types the text of the number such as “two point three zero nine four”
If you wanted to display a decimal as $2.31, you could use this code:
If you wanted to display a decimal like 2nd, you could use this code:
If you wanted to display a decimal like 230.94%, you could use this code:
Finally, if you want to display a decimal like “two point three zero nine four” you could use this code:
For simple conversion of numbers to strings, it’s much simpler to use either the \( ) or String( ) methods, but if you need unique formatting, then use the NumberFormatter class instead.