-1

I have a timer that goes 1.1 1.2 1.3 1.4 etc but then it goes 1.499999999 1.59999999 1.69999999 1.79999999 and on. I need it to stay with the original 2 digits. How do I do this? Here's the update function for my timer:

 func update() {
    // Something Swag
    if timerRunning == true{


        timerCountM += 0.1

        duringPlayTimerLbl.text = "\(timerCountM)"
    }else{

    }

}

2 Answers2

0
let fmt = NSNumberFormatter()
fmt.locale = NSLocale(localeIdentifier: "en_US_POSIX")
fmt.maximumFractionDigits = 2 // Change this to the maximum decimals places
fmt.minimumFractionDigits = 0 // Minimum decimals. Make same as maximum to always have that amount

 func update() {
    // Something Swag
    if timerRunning {

        timerCountM += 0.1

        let formattedTimer = fmt.stringFromNumber(timerCountM)!

        duringPlayTimerLbl.text = "\(formattedTimer)"

    }else{

    }

}
Shades
  • 5,568
  • 7
  • 30
  • 48
0

You can make timerCountM to be Int and do:

timerCountM += 1
text = "\(Double(timerCountM)/10)"
0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68