-3

I'm using Swift 2. I have double myMinute and I want to convert it to double hours(myHours). How can I do it? My codes under below

let myMinute : Double = 62.0
let myHours  : Double = ?

I want to show with math example: myhours = 1.0

halfer
  • 19,824
  • 17
  • 99
  • 186
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85

2 Answers2

1

Found this here!

func minutesToHoursMinutes (minutes : Int) -> (hours : Int , leftMinutes : Int {
return (minutes / 60, (minutes % 60)) 
}

let timeTuple = minutesToHoursMinutes(minutes: 100)

timeTuple.hours  /// 1
timeTuple.leftMinutes /// 40
Daniel Lyon
  • 1,499
  • 10
  • 15
  • dude output must be like , 1.02 – SwiftDeveloper Jul 27 '18 at 07:03
  • @Daniel Lyon output is example 3.723432434, how can i do it only 3.7 ? – SwiftDeveloper Jul 27 '18 at 07:37
  • let roundedHours = Double(String(format: "%.1f", myHours)) @SwiftDeveloper – Daniel Lyon Jul 27 '18 at 07:38
  • I see in this answer you temporarily modified the code, though I don't understand the purpose of so many consecutive rollbacks. If you want to use the expanded version, feel free - it is in [the revision history](https://stackoverflow.com/posts/51552439/revisions). Also, the addition of some introductory/explanatory text as to why this answer solves the problem would be ideal. – halfer Mar 23 '20 at 10:58
1

The value should be 1.03 not 1.02 if you use the current value of minute (you can check with calculator) and it is a simple math, i think what you want beside the calculation is how to round the value to 2 decimal point. You should've made it clearer. This will do the work.

myHours = Double(round(myMinute / 60 * 100) / 100)
print(myHours)
Edy
  • 245
  • 1
  • 7