ios swift正确浮动到字符串转换

时间:2014-06-23 01:37:08

标签: ios swift

所以在Obj-c中这会有不同的做法,但我想知道是否有人知道如何在swift中做到这一点?我不得不问这个让我感到尴尬,但是根本找不到任何文档,如何将float转换为字符串?

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

2 个答案:

答案 0 :(得分:7)

你这样做:

scoreNameLabel.text = "\(convertUserScoreToString)"

String Interpolation

答案 1 :(得分:3)

我们可以使用格式字符串和Swift的String类型,这要归功于Obj-C桥接!

例如:

override func viewDidLoad() {
    super.viewDidLoad()

    let userScore: Double = receivePlayerCard?.playerScore

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

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