adman
adman

Reputation: 408

Swift - How do I display portions of an hour?

I have a label that I'm updating every second which shows the hours with 100ths of an hour("1.67", etc.). I'm trying to get it to show correctly, but having trouble.

    second = 781

    let timeWorked = Float(second/3600)

    let timeWorkedInHours = (String(format:"%.02f", timeWorked))

    timeWorkedLabel.text = "\(timeWorkedInHours)"

I've tried a few things, but nothing that has worked yet. Any solutions?

Upvotes: 0

Views: 29

Answers (1)

Code Different
Code Different

Reputation: 93181

I guess your second is defined as Int. Define it as Double instead. Then you can get rid of the Float cast too:

var second: Double = 0
// ...
let timeWorked = second / 3600.0

Upvotes: 1

Related Questions