swift - Convert decimal to hours:minutes:seconds -
i storing numbers in mysql db doubles can min, max , sums.
i have decimal number 1.66777777778 equals 01:40:04 wanting able convert decimal in hour:minutes:seconds in swift can display value 01:40:04 don't know how.
i have done searching results calculators without explanation.
i have function convert decimal:
func timetohour(hour: string, minute:string, second:string) -> double { var hoursource = 0.00 if hour == "" { hoursource = 0.00 } else { hoursource = double(hour)! } let minutesource = double(minute)! let secondsource = double(second)! let timedecimal: double = hoursource + (minutesource / 60) + (secondsource / 3600) return timedecimal }
but need 1 go other way.
thanks
try:
func hourtostring(hour:double) -> string { let hours = int(floor(hour)) let mins = int(floor(hour * 60) % 60) let secs = int(floor(hour * 3600) % 60) return string(format:"%d:%02d:%02d", hours, mins, secs) }
basically break each component out , concatenate them together.
Comments
Post a Comment