ruby convert hundredth seconds to timestamp optimization
- by Nik
Hey! I want to convert "123456" to "00:20:34.56" where the two digits to the right of the decimal point is in hundredth of a second. So 00:00:00.99 + 00:00:00.01 = 00:00:01.00
What I have:
def to_hmsc(cent)
h = (cent/360000).floor
cent -= h*360000
m = (cent/6000).floor
cent -= m*6000
s = (cent/100).floor
cent -= s*100
"#{h}:#{m}:#{s}.#{s}"
end
does this:
to_hmsc("123456") #= "0:20:34.56"
Question 1: I mean,this is ruby, I find the part ' cent -=... ' rather clunky. Can you see any way to shorten the entire process?
Question 2: This has been asked a million times before, but please share whatever you've got: what's the shortest way to add leading zero to the digits. so that
to_hmsc("123456") #= "00:20:34.56"
Thanks!