Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do to print two zero's 00 as an integer?

Tags:

string

ruby

I'm working on some app academy practice questions and I can't seem to print two 00's for my time conversion. Here's what I have so far:

def time_conversion(minutes)
  hours = minutes/60            
  if minutes%60 < 10
    minutes = minutes%60
  elsif minutes%60 > 10
    minutes = minutes%60
  elsif minutes%60 == 0
    minutes = 00
  end        

  return "#{hours}:#{minutes}"
end

time_conversion(360)
like image 410
EggSix Avatar asked Mar 17 '23 16:03

EggSix


1 Answers

You can use sprintf:

sprintf("%02d:%02d", hours, minutes)

or the equivalent String#%

"%02d:%02d" % [hours, minutes]
like image 128
Yu Hao Avatar answered Mar 20 '23 04:03

Yu Hao