Stephen Fox
Stephen Fox

Reputation: 14470

Convert a float into a string in Swift iOS

I want to convert a Float into a string.

myFloat: Float = 99.0
myString: String

How would I convert myFloat into a string so I can assign the following code

myString = myFloat

Upvotes: 0

Views: 7529

Answers (2)

Ethan
Ethan

Reputation: 1567

Similar to Connor's answer, you can do the following to have a little more control about how your Double or Float is dislpayed...

let myStringToTwoDecimals = String(format:"%.2f", myFloat)

This is basically like stringWithFormat in objC.

Upvotes: 8

Connor
Connor

Reputation: 64644

You can use String Interpolation:

var myFloat: Float = 99.0
var myString: String = "\(myFloat)"

Upvotes: 0

Related Questions