Hi, I want to format an integer as hours and minutes. Of course it''s easy to write this myself, but let''s respect DRY! Is there a method I can use so that integers are converted to strings of 2 digits? So: 2 --> 02 11 --> 11 4 --> 04 thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yep, you can use sprintf:>> sprintf("%0.2d", 4)=> "04" -- Thiago Jackiw acts_as_solr => http://acts-as-solr.rubyforge.org Sitealizer Web Stats => http://sitealizer.rubyforge.org On Mar 28, 3:50 pm, Brij Naald <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I want to format an integer as hours and minutes. > > Of course it''s easy to write this myself, but let''s respect DRY! > > Is there a method I can use so that integers are converted to strings of > 2 digits? > > So: > 2 --> 02 > 11 --> 11 > 4 --> 04 > > thanks! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I want to format an integer as hours and minutes. > > Of course it''s easy to write this myself, but let''s respect DRY! > > Is there a method I can use so that integers are converted to strings of > 2 digits? > > So: > 2 --> 02 > 11 --> 11 > 4 --> 04sprintf("%02d", 4) => 04 -philip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
also,>> i=61; "%02i:%02i" % [i / 60, i % 60]=> "01:01" On Mar 29, 6:50 am, Brij Naald <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I want to format an integer as hours and minutes. > > Of course it''s easy to write this myself, but let''s respect DRY! > > Is there a method I can use so that integers are converted to strings of > 2 digits? > > So: > 2 --> 02 > 11 --> 11 > 4 --> 04 > > thanks! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---