Complete.Org: Mailing Lists: Archives: discussion: October 2000:
[aclug-L] Re: Formatting numbers in Perl!
Home

[aclug-L] Re: Formatting numbers in Perl!

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: discussion@xxxxxxxxx
Subject: [aclug-L] Re: Formatting numbers in Perl!
From: Jeff Schaller <schaller@xxxxxxxxxxxxx>
Date: Fri, 20 Oct 2000 07:36:29 -0500 (CDT)
Reply-to: discussion@xxxxxxxxx

On Fri, 20 Oct 2000, Mohammad Islam wrote:

> Steven Saner wrote:
> > 
> > You can use the sprintf, which is the same as printf except that it
> > will print to a string. So something like:
> > 
> > $sub_total = sprintf "%.2d", $price * $quantity;
> > 
> Thanks. Yea i have found some info about sprintf and alreaduy using it.
> Now how about left or right justifying? Say I have the following..
> 
> 99.99 124.50 6780.98
> 
> I want them to line up like this:
> 
>   99.99
>  124.50
> 6780.98
> 
> Can i do this with sprintf?

Yes. You can also do it with format(), but *shiver* format is
cryptic :)  If you know the width of your widest data, you
can hard-code it as such:

$a=99.99;
$b=124.50;
$c=6780.98;
printf "%7s\n", $a;
printf "%7s\n", $b;
printf "%7s\n", $c;
^D
  99.99
  124.5
6780.98

Left alignment is achieved with: printf "%-7s\n", $a;

By the way, printf can print to a file ... man perlfunc:

       printf FILEHANDLE FORMAT, LIST
       printf FORMAT, LIST


-jeff
-- 
Burns: "It's alive!  Oh, that fellow at Radio Shack said I was mad.  
        Well, who's mad now!  Hah!  Ha ha!  Hahaha!"


-- This is the discussion@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]