Simply Ruby question: "zerofill"

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Simply Ruby question: "zerofill"

Jake Janovetz
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

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Simply Ruby question: "zerofill"

Sam Stephenson
On 12/19/05, Jake Janovetz <[hidden email]> 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
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Simply Ruby question: "zerofill"

s.ross
In reply to this post by Jake Janovetz
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)

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Simply Ruby question: "zerofill"

dandiebolt
In reply to this post by Jake Janovetz
This is what I would use:
 
val=43
n=8
val2="%0#{n}d" % [val]
 

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails

Reply | Threaded
Open this post in threaded view
|

Re: Simply Ruby question: "zerofill"

Kent Sibilev
In reply to this post by Jake Janovetz
$ irb
irb(main):001:0> "%08d" % [43]
=> "00000043"

Kent.

On Dec 19, 2005, at 1:47 AM, 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails

_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Simply Ruby question: "zerofill"

Jake Janovetz
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?

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Re: Simply Ruby question: "zerofill"

dandiebolt
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"

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails

Reply | Threaded
Open this post in threaded view
|

Re: Re: Simply Ruby question: "zerofill"

Wilson Bilkovich
In reply to this post by Jake Janovetz
On 12/19/05, Jake Janovetz <[hidden email]> 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.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Re: Simply Ruby question: "zerofill"

Ken Bowley-3
In reply to this post by Jake Janovetz
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
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails