Forum: Ruby on Rails Simply Ruby question: "zerofill"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Jake Janovetz (Guest)
on 2005-12-19 07:47
Hi there-

What's the easiest/most efficient way to perform a zerofill in Ruby?
i.e.  Given the value 'val', I would like to do something like:

val = 43
puts val.zerofill(8)
--->  "00000043"

gsub?  sprintf of some sort?

   Jake
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2005-12-19 08:03
Jake Janovetz wrote:
> Hi there-
>
> What's the easiest/most efficient way to perform a zerofill in Ruby?
> i.e.  Given the value 'val', I would like to do something like:
>
> val = 43
> puts val.zerofill(8)
> --->  "00000043"
>
> gsub?  sprintf of some sort?
>
>    Jake

sprintf("%08d", val)
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2005-12-19 08:04
(Received via mailing list)
This is what I would use:

  val=43
n=8
val2="%0#{n}d" % [val]
D928bf38f5616895b0504abb93dca6df?d=identicon&s=25 Kent Sibilev (Guest)
on 2005-12-19 08:07
(Received via mailing list)
$ irb
irb(main):001:0> "%08d" % [43]
=> "00000043"

Kent.
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Jake Janovetz (Guest)
on 2005-12-19 16:26
Kent Sibilev wrote:
> $ irb
> irb(main):001:0> "%08d" % [43]
> => "00000043"
>
> Kent.

Thanks much.  Where is this syntax documented?  Is it just a convenient
way to call sprintf or is it another method?
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2005-12-19 16:35
(Received via mailing list)
ri String#%

  C:\Documents and Settings\Owner\Desktop\MyRuby\Quiz\59>ri String#%
--------------------------------------------------------------- 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"
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2005-12-19 16:38
(Received via mailing list)
On 12/19/05, Jake Janovetz <jakejanovetz@yahoo.com> wrote:
> Kent Sibilev wrote:
> > $ irb
> > irb(main):001:0> "%08d" % [43]
> > => "00000043"
> >
> > Kent.
>
> Thanks much.  Where is this syntax documented?  Is it just a convenient
> way to call sprintf or is it another method?
>
It's a call to sprintf, or I suppose more accurately, a call to:
sprintf(format_string, *values)
The parameter to the right of the % sign can either be a single value,
or an Array, if there are multiple positions to fill in the format
string.
Other than that, it's identical to sprintf.

--Wilson.
55742d912a9003080d202c5522d4de00?d=identicon&s=25 Ken Bowley (Guest)
on 2005-12-19 16:59
(Received via mailing list)
On Mon, 19 Dec 2005, Jake Janovetz wrote:

> Kent Sibilev wrote:
>> $ irb
>> irb(main):001:0> "%08d" % [43]
>> => "00000043"
>>
>> Kent.
>
> Thanks much.  Where is this syntax documented?  Is it just a convenient
> way to call sprintf or is it another method?

http://www.rubycentral.com/book/ref_c_string.html#String._pc
5b9fe87ec1faa67a4599782930f45ec9?d=identicon&s=25 Sam Stephenson (Guest)
on 2005-12-21 21:48
(Received via mailing list)
On 12/19/05, Jake Janovetz <jakejanovetz@yahoo.com> wrote:
> Hi there-
>
> What's the easiest/most efficient way to perform a zerofill in Ruby?
> i.e.  Given the value 'val', I would like to do something like:
>
> val = 43
> puts val.zerofill(8)
> --->  "00000043"
>
> gsub?  sprintf of some sort?

>> "%08d" % 43
=> "00000043"

--
sam
This topic is locked and can not be replied to.