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: Steven Saner <ssaner@xxxxxxxxxxxxxxx>
Date: Fri, 20 Oct 2000 09:06:54 -0500
Reply-to: discussion@xxxxxxxxx

On Fri, Oct 20, 2000 at 07:36:29AM -0500, Jeff Schaller wrote:
> 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;


For your purpose I would suggest:  printf "%7.2s"

That way you force two digits of precision to the right of the decimal point.

Steve

-- 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]