# Turning Numbers Into Strings

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:

var stringHere : String

stringHere = "This is a string = \(aDecimal)"

print (stringHere)

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:

var stringHere : String

stringHere = "This is a string = " + String(aDecimal)

print (stringHere)

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:

let formatMe = NumberFormatter()

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:

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2

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:

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2
formatMe.roundingMode = .up

To retrieve a string from a number, you must use a number of the NSNumber data type such as:

let aDecimal : NSNumber = 2.3094

Now you’ll be able to convert a number into a string using this command:

So the whole code would look like this:

let aDecimal : NSNumber = 2.3094

var stringHere : String

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2
formatMe.roundingMode = .up

stringHere = "This is a string = " + formatMe.string(from: aDecimal)!

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:

let aDecimal : NSNumber = 2.3094

var stringHere : String

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2
formatMe.roundingMode = .up

formatMe.numberStyle = .currency
stringHere = "This is a string = " + formatMe.string(from: aDecimal)!

If you wanted to display a decimal like 2nd, you could use this code:

let aDecimal : NSNumber = 2.3094

var stringHere : String

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2
formatMe.roundingMode = .up

formatMe.numberStyle = .ordinal
stringHere = "This is a string = " + formatMe.string(from: aDecimal)!

If you wanted to display a decimal like 230.94%, you could use this code:

let aDecimal : NSNumber = 2.3094

var stringHere : String

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2
formatMe.roundingMode = .up

formatMe.numberStyle = .percent
stringHere = "This is a string = " + formatMe.string(from: aDecimal)!

Finally, if you want to display a decimal like “two point three zero nine four” you could use this code:

let aDecimal : NSNumber = 2.3094

var stringHere : String

let formatMe = NumberFormatter()
formatMe.maximumFractionDigits = 2
formatMe.roundingMode = .up

formatMe.numberStyle = .spellOut
stringHere = "This is a string = " + formatMe.string(from: aDecimal)!

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.

January 20th, 2017 by