[Chicago-talk] (no subject)
imran javaid
imranjj at gmail.com
Fri Apr 27 21:26:35 PDT 2012
I haven't tested this for all cases, but this should fix the issue for most
cases:
sub myround2 {
my $src = shift;
my $pre = shift;
print int($src * 10**$pre + .5)/100;
print "\n";
}
myround2("1.335",2);
myround2("0.335",2);
myround2("1.334999999999999",2);
myround2("0.334999999999999",2);
-imran
On Fri, Apr 27, 2012 at 8:54 PM, Michael Potter <michael at potter.name> wrote:
> Monks,
>
> I am looking for a graceful way to round numbers.
>
> The following program
> #!/bin/perl
>
> sub myround
> {
> my $src = shift;
> printf("%.2f\n", $src);
> }
>
> myround("1.335");
> myround("0.335");
>
> outputs this:
> $ perl ./round.pl
> 1.33
> 0.34
>
> I know why. It has to do with the well known side effects of using
> floating point.
>
> 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.
>
> --
> potter
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20120427/b472d633/attachment.html>
More information about the Chicago-talk
mailing list