preceding zero

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

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?

Craig White wrote:

Craig White 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

Craig White wrote:

Craig White 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

This isn't another way, it's the exact same way.

--------------------------------------------------------------- String#%
      str % arg => new_str

Craig White wrote:

Craig White wrote:

Craig White 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.

    &quot;%05d&quot; % 123                       \#=&gt; &quot;00123&quot;
    &quot;%\-5s: %08x&quot; % \[ &quot;ID&quot;, self\.id \]   \#=&gt; &quot;ID   : 200e14d6&quot;

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 :wink:

Thanks for your help.

Best Regards,
Michael