user5740195
user5740195

Reputation:

2 decimal timer in swift

I am making a game that has a timer. I have the number increasing at intervals of 0.01 seconds but the number that comes up does not have the decimal point in it. How do I add this decimal point in? What I get after 1 second of the timer running is "100" instead of "1.00"

Here is the code I used:

//timer
{
    var timer = NSTimer()
    var counter = 0

    countingLabel.text = String(counter)
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}

//------timer update-----
func updateCounter() {

    countingLabel.text = String(counter++)

    if score.text == "10" {
       timer.invalidate()
    }
}

Upvotes: 1

Views: 506

Answers (1)

yesthisisjoe
yesthisisjoe

Reputation: 2035

Try replacing

countingLabel.text = String(counter++)

with

counter++
countingLabel.text = String(Double(counter)/100)

Upvotes: 1

Related Questions