Number formatting

Discussion in 'Ruby' started by Cory, May 31, 2007.

  1. Cory

    Cory Guest

    folks - can't quite figure this one out...

    foo = 5
    bar = 10


    basically - when i go to print out the numbers - i want to format 'foo'
    with a leding zero so that it reads '05' and is aesthetically pleasing
    when compared to a double-digit number - such as bar: '10'.


    essentially looking to get:

    05
    10
    25
    03

    --
    Posted via http://www.ruby-forum.com/.
    Cory, May 31, 2007
    #1
    1. Advertising

  2. On Fri, Jun 01, 2007 at 01:03:26AM +0900, Cory wrote:
    > folks - can't quite figure this one out...
    >
    > foo = 5
    > bar = 10
    >
    >
    > basically - when i go to print out the numbers - i want to format 'foo'
    > with a leding zero so that it reads '05' and is aesthetically pleasing
    > when compared to a double-digit number - such as bar: '10'.
    >
    >
    > essentially looking to get:
    >
    > 05
    > 10
    > 25
    > 03


    foo = 5
    puts "%02d" % foo


    --------------------------------------------------------------- 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"
    Brian Candler, May 31, 2007
    #2
    1. Advertising

  3. On 5/31/07, Cory <> wrote:
    > folks - can't quite figure this one out...
    >
    > foo = 5
    > bar = 10
    >
    > basically - when i go to print out the numbers - i want to format 'foo'
    > with a leding zero so that it reads '05' and is aesthetically pleasing
    > when compared to a double-digit number - such as bar: '10'.


    Try using Kernel#printf.

    irb(main):001:0> printf("%02d", 5)
    05=> nil


    --
    Luis Parravicini
    http://ktulu.com.ar/blog/
    Luis Parravicini, May 31, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rguti

    Number formatting

    rguti, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    401
    Craig Deelsnyder
    Jun 10, 2004
  2. Luis Esteban Valencia

    Formatting decimal number

    Luis Esteban Valencia, Jan 12, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    530
    Philip Q [MVP]
    Jan 12, 2005
  3. probashi

    Number Formatting Question C#

    probashi, Feb 23, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    15,975
    Karl Seguin
    Feb 23, 2005
  4. thomson

    Number Formatting

    thomson, Aug 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    360
  5. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    501
    Karl Seguin
    Nov 22, 2005
Loading...

Share This Page