hi. I want to format a Integer value to a hour with a preceding zero.(two digits) eg. 8 => 08 Exists a Ruby or Rails function for that? thanks Michael -- Posted via http://www.ruby-forum.com/.
On Sun, 2009-09-27 at 19:26 +0200, Michael .. wrote:> hi. > > I want to format a Integer value to a hour with a preceding zero.(two > digits) > eg. 8 => 08 > > Exists a Ruby or Rails function for that? >---- you can easily create your own function and put it into application.rb or the helper myvar < 10 ? "0" + myvar.to_s : myvar.to_s it must be a string to retain the leading zero Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Craig White wrote:> On Sun, 2009-09-27 at 19:26 +0200, Michael .. wrote: > >> hi. >> >> I want to format a Integer value to a hour with a preceding zero.(two >> digits) >> eg. 8 => 08 >> >> Exists a Ruby or Rails function for that? >> >> > ---- > you can easily create your own function and put it into application.rb > or the helper > > myvar < 10 ? "0" + myvar.to_s : myvar.to_s > > it must be a string to retain the leading zeroor "%02d" % myvar Cheers, Mohit. 9/28/2009 | 1:52 AM.
On Mon, 2009-09-28 at 01:52 +0800, Mohit Sindhwani wrote:> Craig White wrote: > > On Sun, 2009-09-27 at 19:26 +0200, Michael .. wrote: > > > >> hi. > >> > >> I want to format a Integer value to a hour with a preceding zero.(two > >> digits) > >> eg. 8 => 08 > >> > >> Exists a Ruby or Rails function for that? > >> > >> > > ---- > > you can easily create your own function and put it into application.rb > > or the helper > > > > myvar < 10 ? "0" + myvar.to_s : myvar.to_s > > > > it must be a string to retain the leading zero > > or "%02d" % myvar---- ok - you win Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Craig White wrote:> On Mon, 2009-09-28 at 01:52 +0800, Mohit Sindhwani wrote: >> Craig White wrote: >>> On Sun, 2009-09-27 at 19:26 +0200, Michael .. wrote: >>> >>>> hi. >>>> >>>> I want to format a Integer value to a hour with a preceding zero.(two >>>> digits) >>>> eg. 8 => 08 >>>> >>>> Exists a Ruby or Rails function for that? >>>> >>>> >>> ---- >>> you can easily create your own function and put it into application.rb >>> or the helper >>> >>> myvar < 10 ? "0" + myvar.to_s : myvar.to_s >>> >>> it must be a string to retain the leading zero >> or "%02d" % myvar > ---- > ok - you win > > Craig > >yes another way irb(main):024:0> val = 8 => 8 irb(main):025:0> str = sprintf( "%02d", val ) => "08" irb(main):026:0> p str "08" => nil -- Kind Regards, Rajinder Yadav http://DevMentor.org Do Good ~ Share Freely
On Sep 27, 2009, at 2:26 PM, Rajinder Yadav wrote:> Craig White wrote: >> On Mon, 2009-09-28 at 01:52 +0800, Mohit Sindhwani wrote: >>> Craig White wrote: >>>> On Sun, 2009-09-27 at 19:26 +0200, Michael .. wrote: >>>> >>>>> hi. >>>>> >>>>> I want to format a Integer value to a hour with a preceding zero. >>>>> (two >>>>> digits) >>>>> eg. 8 => 08 >>>>> >>>>> Exists a Ruby or Rails function for that? >>>>> >>>> ---- >>>> you can easily create your own function and put it into >>>> application.rb >>>> or the helper >>>> >>>> myvar < 10 ? "0" + myvar.to_s : myvar.to_s >>>> >>>> it must be a string to retain the leading zero >>> or "%02d" % myvar >> ---- >> ok - you win >> >> Craig > > yes another way > > irb(main):024:0> val = 8 > => 8 > irb(main):025:0> str = sprintf( "%02d", val ) > => "08" > irb(main):026:0> p str > "08" > => nil > > -- > Kind Regards, > Rajinder Yadav > > http://DevMentor.org > Do Good ~ Share FreelyThis isn''t another way, it''s the exact same way. --------------------------------------------------------------- String#% str % arg => new_str ------------------------------------------------------------------------ Format---Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array containing the values to be substituted. See Kernel::sprintf for details of the format string. "%05d" % 123 #=> "00123" "%-5s: %08x" % [ "ID", self.id ] #=> "ID : 200e14d6" The String class just makes is a bit easier to type. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Craig White wrote:> On Mon, 2009-09-28 at 01:52 +0800, Mohit Sindhwani wrote: > >> Craig White wrote: >> >>>> >>>> >>> ---- >>> you can easily create your own function and put it into application.rb >>> or the helper >>> >>> myvar < 10 ? "0" + myvar.to_s : myvar.to_s >>> >>> it must be a string to retain the leading zero >>> >> or "%02d" % myvar >> > ---- > ok - you win > > Craig >Hardly :) I just use that a lot!! A lot of my work involves data conversion and when outputting it, sending out 7 or 10 decimal places looks clumsy. So, it''s short hand that I''ve become accustomed to writing :) Cheers, Mohit. 9/28/2009 | 11:48 AM.
On Sun, Sep 27, 2009 at 10:58 PM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> > On Sep 27, 2009, at 2:26 PM, Rajinder Yadav wrote: >> Craig White wrote: >>> On Mon, 2009-09-28 at 01:52 +0800, Mohit Sindhwani wrote: >>>> Craig White wrote: >>>>> On Sun, 2009-09-27 at 19:26 +0200, Michael .. wrote: >>>>> >>>>>> hi. >>>>>> >>>>>> I want to format a Integer value to a hour with a preceding zero. >>>>>> (two >>>>>> digits) >>>>>> eg. 8 => 08 >>>>>> >>>>>> Exists a Ruby or Rails function for that? >>>>>> >>>>> ---- >>>>> you can easily create your own function and put it into >>>>> application.rb >>>>> or the helper >>>>> >>>>> myvar < 10 ? "0" + myvar.to_s : myvar.to_s >>>>> >>>>> it must be a string to retain the leading zero >>>> or "%02d" % myvar >>> ---- >>> ok - you win >>> >>> Craig >> >> yes another way >> >> irb(main):024:0> val = 8 >> => 8 >> irb(main):025:0> str = sprintf( "%02d", val ) >> => "08" >> irb(main):026:0> p str >> "08" >> => nil >> >> -- >> Kind Regards, >> Rajinder Yadav >> >> http://DevMentor.org >> Do Good ~ Share Freely > > > This isn''t another way, it''s the exact same way. > > --------------------------------------------------------------- String#% > str % arg => new_str > ------------------------------------------------------------------------ > Format---Uses str as a format specification, and returns the > result of applying it to arg. If the format specification contains > more than one substitution, then arg must be an Array containing > the values to be substituted. See Kernel::sprintf for details of > the format string. > > "%05d" % 123 #=> "00123" > "%-5s: %08x" % [ "ID", self.id ] #=> "ID : 200e14d6" > > The String class just makes is a bit easier to type.Rob you''re right its the same thing, different syntax, me thinks my C++ style is showing ;)> > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > > > >-- Kind Regards, Rajinder Yadav May the { source } be with you !
Thanks for your help. Best Regards, Michael -- Posted via http://www.ruby-forum.com/.
str.ljust(integer, padstr='' '') => new_str If integer is greater than the length of str, returns a new String of length integer with str left justified and padded with padstr; otherwise, returns str. "hello".ljust(4) #=> "hello" "hello".ljust(20) #=> "hello " "hello".ljust(20, ''1234'') #=> "hello123412341234123" I don''t need to paste the doc for ''rjust'', do I? ;-) -- Posted via http://www.ruby-forum.com/.