Extremely simple printf question

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

Got it - sorry.

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

Wes G. 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.