Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > printf doesn't do right justify

Reply
Thread Tools

printf doesn't do right justify

 
 
Daniel Cutter
Guest
Posts: n/a
 
      03-09-2008
I thought that printf would allways right justify numbers, unless told
to do differently (with '-'). perlfunc doesn't tell.

#!perl
use strict;
use warnings;
printf("%2.1f\n", 12.3);
printf("%2.1f\n", 4.5);
printf("%02.1f\n", 6.7);

The results are:
12.3
4.5
6.7

But I expected:
12.3
4.5
06.7

What am I missing?

perl -v says:
This is perl, v5.10.0 built for MSWin32-x86-multi-thread
It's Win XP with all the patches

Regards
Daniel Cutter
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-09-2008

Quoth Daniel Cutter <(E-Mail Removed)_I S_DE>:
> I thought that printf would allways right justify numbers, unless told
> to do differently (with '-'). perlfunc doesn't tell.
>
> #!perl
> use strict;
> use warnings;
> printf("%2.1f\n", 12.3);
> printf("%2.1f\n", 4.5);
> printf("%02.1f\n", 6.7);


The number before the . is the minimum total field width, not the number
of places before the decimal (yes, I think this is confusing too, but
it's more consistent with %3.5s). So you want

printf("%4.1f\n", 12.3);
printf("%4.1f\n", 4.5);
printf("%04.1f\n", 6.7);

Ben

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf: Centre justify data vashwath@rediffmail.com C Programming 2 12-02-2005 10:32 AM
Right justify text in textbox in web page John Wilson ASP .Net 2 10-08-2005 09:38 PM
right justify a column? John Wilson ASP .Net 2 06-22-2005 06:24 PM
Right Justify Hyperlink Buttons? Tina ASP .Net 2 12-09-2004 05:27 PM
How to right justify a column jwatts@berkeley.edu C Programming 2 07-23-2003 06:59 AM



Advertisments