[Chicago-talk] (no subject)

Alexander Danel danel at speakeasy.net
Sat Apr 28 13:14:40 PDT 2012

The correct way to round numbers in Perl is "printf()" and "sprintf()".

Note that rounding is something that makes sense to humans but not to
computers.  Computers are binary, they don't care about our decimal
concepts, unless we ask them to.  The times when decimal numbers matter is
during input (from a human) and output (for a human.)  Therefore, it is
appropriate that the Perl method for rounding is an output (or string)

Alexander Danel

-----Original Message-----
From: chicago-talk-bounces+danel=speakeasy.net at pm.org
[mailto:chicago-talk-bounces+danel=speakeasy.net at pm.org] On Behalf Of
Michael Potter
Sent: Friday, April 27, 2012 8:55 PM
To: Chicago.pm chatter
Subject: [Chicago-talk] (no subject)


I am looking for a graceful way to round numbers.

The following program

sub myround
   my $src = shift;
   printf("%.2f\n", $src);


outputs this:
$ perl ./round.pl

I know why.  It has to do with the well known side effects of using floating

What I don't know is a graceful way to round.

I would like to use something that already comes with perl and void adding
any modules.

Chicago-talk mailing list
Chicago-talk at pm.org

More information about the Chicago-talk mailing list