On Mon, 06 Dec 2010 22:07:17 -0600
Harry Putnam <rea...@newsguy.com> wrote:

> Is there any way to reduce the amount of decimals math might be
> carried too short of printf?
> 
> I always find printf to end up taking a fair bit of time, since I've
> forgotten most of what I ever knew about it each time I run into a
> need for it.
> 
> I just wondered if there is any other kind of limiting mechanism?
> 
> It would be handy if simple math like:
> 
>   my $ans = (261 / 59.00)
> 
> could be made to show only 2 decimal places like:
>    4.42
> 
> instead of:
>    4.42372881355932



Hi,

Do a  perldoc -f sprintf

The first few lines are;

===========================================================
sprintf FORMAT, LIST

Returns a string formatted by the usual "printf" conventions of the C
library function "sprintf".  See below for more details and see
sprintf(3) or printf(3) on your system for an explanation of the
general principles.

For example:

                       # Format number with up to 8 leading zeroes
                       $result = sprintf("%08d", $number);

                       # Round number to 3 digits after decimal point
                       $rounded = sprintf("%.3f", $number);
===========================================================



Owen

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to