user3746995
user3746995

Reputation: 57

ios swift proper float to string conversion

So in Obj-c this would be done differently, but I was wondering if anyone had any idea how to do this in swift? I feel embarrased having to ask this, but simply cannot find any documentation on it, how do I convert a float to string?

override func viewDidLoad() {
    super.viewDidLoad()

    var userScore = receivePlayerCard?.playerScore

    var convertUserScoreToString: Float

    scoreNameLabel.text = convertUserScoreToString //need to convert it to a string here
    scoreNameLabel.textColor = UIColor.whiteColor()
    // Do any additional setup after loading the view.
}

Upvotes: 2

Views: 7631

Answers (2)

fqdn
fqdn

Reputation: 2843

We can use format strings with Swift's String type, thanks to Obj-C bridging!

So for example:

override func viewDidLoad() {
    super.viewDidLoad()

    let userScore: Double = receivePlayerCard?.playerScore

    scoreNameLabel.text = String(format: "%.2f", userScore)

    // Do any additional setup after loading the view.
}

Upvotes: 2

Connor
Connor

Reputation: 64674

You'd do it like this:

scoreNameLabel.text = "\(convertUserScoreToString)"

String Interpolation

Upvotes: 7

Related Questions