[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Extremely simple printf question

Wes Gamble

9/29/2006 7:22:00 PM

How embarrassing - how long have I been away from C to have to ask this?

I want to display a float with only two digits of precision.

I'm trying this:

irb(main):011:0> printf("%2f", 5.3456)
5.345600=> nil

What am I doing wrong?

Wes

--
Posted via http://www.ruby-....

2 Answers

Wes Gamble

9/29/2006 7:24:00 PM

0

Got it - sorry.

irb(main):014:0> printf("%-10.2f", 5.3456)
5.35 => nil


--
Posted via http://www.ruby-....

Paul Lutus

9/29/2006 8:39:00 PM

0

Wes Gamble wrote:

> Got it - sorry.

Actually, you didn't, quite.

>
> irb(main):014:0> printf("%-10.2f", 5.3456)
> 5.35 => nil

You almost always want to right-justify a column of such numbers:

irb(main):014:0> printf("%10.2f", 5.3456)

Use the default form without the "-", add a field width larger than the
largest number, and you will get a nice right-justified column.

--
Paul Lutus
http://www.ara...