Gabe12 Gabe12 - 5 months ago 47
Swift Question

Keeping score in XCode using swift

So I want to keep a

Score
in an XCode project. But I want it to be like
0.01, 0.02...
until it gets to
1.00
and from
1
to continue like
1, 2, 3...


My current code looks like this:

var Highscore = 0
var Score = 0

...

@IBAction func Action(sender: AnyObject) {

if (Score < 100) {

Score++
HighScoreLabel.text = "HighScore:\(Score)"

} else if (Score >= 100) {

Score += 100
HighScoreLabel.text = "HighScore:\(Score)"


I've already tried creating a variables which divides Score, I've tried to make it a double (but I want
1, 2..
not
1.00, 2.00
), and I don't know what to do.
Please help me if you can, and feel free to edit.

Answer
func action() {
    if (score < 100) {
        score += 1
        let someScore = score < 1 ? String(format: "%.2f", score) : String(format: "%.f", round(score))
        println("HighScore:\(someScore)")
    } else if (score >= 100) {
        score += 100
        let someScore = score < 1 ? String(format: "%.2f", score) : String(format: "%.0f", round(score))
        println("HighScore:\(someScore)")
    }
}

This should do it.

EDIT:
Used your code.