[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

numbers output formatting

Vladimir Agafonkin

6/21/2006 9:50:00 AM

Hi.

How can I pad a number with "0"-s to a speciefied length when it goes
to the output? It's a very simple newbie question, yet I haven't been
able to find an answer in Programming Ruby book and core library docs.

Say, we have a following code:

for i in 1..10 do
puts "num #{i}"
end

What I need to change in this little script to format the output so
that it looks like this?

num 01
num 02
....
num 10

Thanks in advance.

8 Answers

Robert Klemme

6/21/2006 10:06:00 AM

0

Vladimir Agafonkin wrote:
> Hi.
>
> How can I pad a number with "0"-s to a speciefied length when it goes
> to the output? It's a very simple newbie question, yet I haven't been
> able to find an answer in Programming Ruby book and core library docs.
>
> Say, we have a following code:
>
> for i in 1..10 do
> puts "num #{i}"
> end
>
> What I need to change in this little script to format the output so
> that it looks like this?
>
> num 01
> num 02
> ...
> num 10
>
> Thanks in advance.
>

http://www.ruby-doc.org/core/classes/Kernel.ht...

robert

greg.rb

6/21/2006 1:43:00 PM

0

just one way to do it:

for i in 1..10 do
printf("%02d \n", i)
end

c documentation has more on printf

greg.rb

6/21/2006 1:46:00 PM

0


greg.rb wrote:
> just one way to do it:
>
> for i in 1..10 do
> printf("%02d \n", i)
> end
>
> c documentation has more on printf
i forgot to put in 'num':

for i in 1..10 do
printf("num %02d \n", i)
end

produces:
num 01
num 02
num 03
num 04
num 05
num 06
num 07
num 08
num 09
num 10

ColorMystic

6/21/2006 4:51:00 PM

0

I believe Java 5 has added printf as well.

Si

6/22/2006 12:02:00 PM

0


Vladimir Agafonkin wrote:
> Hi.
>
> How can I pad a number with "0"-s to a speciefied length when it goes
> to the output? It's a very simple newbie question, yet I haven't been
> able to find an answer in Programming Ruby book and core library docs.
>
> Say, we have a following code:
>
> for i in 1..10 do
> puts "num #{i}"
> end
>
> What I need to change in this little script to format the output so
> that it looks like this?
>
> num 01
> num 02
> ...
> num 10
>
> Thanks in advance.

also:

(1..10).each do |i|
puts "num #{'%02d' % i}"
end

Austin Ziegler

6/22/2006 12:06:00 PM

0

Si wrote:
> (1..10).each do |i|
> puts "num #{'%02d' % i}"
> end

(1..10).each do |i|
puts "num %02d" % i
end

-a

Karl von Laudermann

6/22/2006 1:22:00 PM

0

Bah, printf() is for wimps.

for i in 1..10 do
puts "num " + ("0" * (1 - Math.log10(i).truncate)) + i.to_s
end

Just increase the 1 to increase the number of zeros. :-)

Dumaiu

6/22/2006 3:36:00 PM

0

Karl von Laudermann wrote:
> Bah, printf() is for wimps.
>
> for i in 1..10 do
> puts "num " + ("0" * (1 - Math.log10(i).truncate)) + i.to_s
> end
>
> Just increase the 1 to increase the number of zeros. :-)

Better make sure he knows that was a joke!