Click here to get back home

Re: printf doesn't do right justify

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Re: printf doesn't do right justify Ben Morrow 03-09-2008
Posted by Ben Morrow on March 9, 2008, 8:32 am
Please log in for more thread options

> 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


Similar ThreadsPosted
printf doesn't do right justify March 9, 2008, 8:04 am
justify text and PDF::API2 module February 17, 2005, 2:26 am
printf and variable length? October 25, 2005, 10:03 am
Using printf to modify scalars May 19, 2006, 8:44 am
printf: zero pad after the decimal a given amount March 30, 2008, 4:09 pm
Can't find right printf format April 28, 2008, 1:45 pm
printf + escape code May 22, 2008, 12:36 am
perl win32 & output of printf November 5, 2004, 4:15 pm
To store the output of printf into a variable... April 3, 2005, 9:56 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap