Forum: Ruby [noob question] convert 7 in 07

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.
C388f6c1fd2bf8c651678e14b78aefa7?d=identicon&s=25 Paul A. (pulu)
on 2009-04-22 12:38
Hi,

I would like to display numbers with a 0 before when there is only one
char.
For exemple if I gets the number 4, I would like to return 04.

After looking methods on
http://www.ruby-doc.org/core/classes/Fixnum.html, I don't see how to
process. Of cours it is possible to convert a number into string and
after add a 0, but it's not very friendly.

Thanks for ideas
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-22 12:45
(Received via mailing list)
2009/4/22 Paul A. <cyril.staff@gmail.com>:
> I would like to display numbers with a 0 before when there is only one
> char.
> For exemple if I gets the number 4, I would like to return 04.
>
> After looking methods on
> http://www.ruby-doc.org/core/classes/Fixnum.html, I don't see how to
> process. Of cours it is possible to convert a number into string and
> after add a 0, but it's not very friendly.

Please look up sprintf, printf and String#%.

Cheers

robert
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-04-22 12:52
counter = 1; sprintf("%02d", counter) # => "01"

In general, the ruby String class is much better than Int class -
because Strings are like everywhere in ruby. I believe Matz once said
that the String class was most work (or devoured most manhours when he
worked on ruby initially)
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2009-04-22 13:00
(Received via mailing list)
>
And keep in mind that you will have to convert to string anyway,
because while 07 is fine
08 and 09 are illegal in Ruby—it treats numbers with leading zero as
octal numbers:

>> 7
=> 7
>> 07
=> 7
>> 8
=> 8
>> 08
SyntaxError: (irb):4: Invalid octal digit from /usr/local/bin/irb:12:in
`<main>'

Regards,
Rimantas
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-04-22 13:33
(Received via mailing list)
irb(main):001:0> n=7
=> 7
irb(main):002:0> "%02d" % n
=> "07"

--- On Wed, 4/22/09, Paul A. <cyril.staff@gmail.com> wrote:
From: Paul A. <cyril.staff@gmail.com>
Subject: [noob question] convert 7 in 07
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Date: Wednesday, April 22, 2009, 6:39 AM

Hi,

I would like to display numbers with a 0 before when there is only one
char.
For exemple if I gets the number 4, I would like to return 04.

After looking methods on
http://www.ruby-doc.org/core/classes/Fixnum.html, I don't see how to
process. Of cours it is possible to convert a number into string and
after add a 0, but it's not very friendly.

Thanks for ideas
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-04-22 13:50
(Received via mailing list)
On Apr 22, 6:58 am, "DanDiebolt.exe" <dandieb...@yahoo.com> wrote:
> To: "ruby-talk ML" <ruby-t...@ruby-lang.org>
> Date: Wednesday, April 22, 2009, 6:39 AM
>
> Hi,
>
> I would like to display numbers with a 0 before when there is only one
> char.
> For exemple if I gets the number 4, I would like to return 04.
>
> After looking methods onhttp://www.ruby-doc.org/core/classes/Fixnum.html, I don't see 
how to
> process.

Dan's suggestion is the simplest. But I want to point out that you can
add a Fixnum method yourself, if you are so inclined:

class Fixnum
  def to_formatted_s
    "%02d" % self
  end
end

puts 7.to_formatted_s
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-04-22 14:33
(Received via mailing list)
>*'s suggestion is the simplest

Well I just picked it up on one of the n-tips for ruby lists. This isn't
the list I originally studied but it does contain the same idea in tip
#9:

9. Pretty print strings decimals. The format descriptors are pretty
much similar to those used in C’s printf so I won’t go into details.

my_string = "%01d %06d %02d" % [type, code, age]
http://alexbrie.net/2002/10-ruby-programming-tips-...

These lists are an invaluable resource to improve your code.
This topic is locked and can not be replied to.