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/.
On 12/19/05, Jake Janovetz <jakejanovetz-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> 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
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? > > Jakesprintf("%08d", val) -- Posted via http://www.ruby-forum.com/.
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 Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
$ 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 > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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/.
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 Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/19/05, Jake Janovetz <jakejanovetz-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> 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.
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