in Ruby, conversion of float integer into %H %M %S time

How do you convert a float, say 13.5, to the corresponding 24-hour time %H:%M:%S? (13.5 would be 13:30:00, 8.25 would be 8:15:00) I'm still figuring the Time class...it confuses me

sec = (13.5 * 3600).to_i
min, sec = sec.divmod(60)
hour, min = min.divmod(60)
"%02d:%02d:%02d" % [hour, min, sec] # => "13:30:00"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章