在Ruby中,将float整数转换为%H%M%S时

时间:2014-04-07 05:53:04

标签: ruby time

如何将浮点数(例如13.5)转换为相应的24小时时间%H:%M:%S? (13.5将是13:30:00,8.25将是8:15:00) 我还在想时间课......它让我感到困惑

2 个答案:

答案 0 :(得分:4)

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"

答案 1 :(得分:0)

#Time of your with two digit in minutes like 5 to 50
time_val="13.50"
#split by "."
split_val=time_val.split('.')
#get a hours 
hour_val=split_val[0]
#get a minutes
min_val= (100*60)/split_val[1].to_i
if(min_val == 60)
   hour_val +=1
   min_val=0
end
#it's concat a hours and minutes then it's final result
final_time= hour_val.to_s + ":" + min_val + ":00"