<div class="gmail_quote">On Sat, Apr 28, 2012 at 3:14 PM, Alexander Danel <span dir="ltr"><<a href="mailto:danel@speakeasy.net" target="_blank">danel@speakeasy.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The correct way to round numbers in Perl is "printf()" and "sprintf()".<br>
<br>
Note that rounding is something that makes sense to humans but not to<br>
computers.  Computers are binary, they don't care about our decimal<br>
concepts, unless we ask them to.  The times when decimal numbers matter is<br>
during input (from a human) and output (for a human.)  Therefore, it is<br>
appropriate that the Perl method for rounding is an output (or string)<br>
function.<br>
<span class="HOEnZb"><font color="#888888"><br>
Alexander Danel<br></font></span></blockquote><div><br>Your observation that rounding really only makes sense with a string 
output is a good point that I had never quite realized. However, printf and sprintf do not give you much control over the rounding details (which, though esoteric, are important). In case you missed it, Michael posted that he found a CPAN module that gave him exactly what he needed, called Math::Decimal.<br>
<br>David<br><br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="HOEnZb"><font color="#888888">
</font></span><div class="HOEnZb"><div class="h5">
-----Original Message-----<br>
From: chicago-talk-bounces+danel=<a href="mailto:speakeasy.net@pm.org">speakeasy.net@pm.org</a><br>
[mailto:<a href="mailto:chicago-talk-bounces%2Bdanel">chicago-talk-bounces+danel</a>=<a href="mailto:speakeasy.net@pm.org">speakeasy.net@pm.org</a>] On Behalf Of<br>
Michael Potter<br>
Sent: Friday, April 27, 2012 8:55 PM<br>
To: Chicago.pm chatter<br>
Subject: [Chicago-talk] (no subject)<br>
<br>
Monks,<br>
<br>
I am looking for a graceful way to round numbers.<br>
<br>
The following program<br>
#!/bin/perl<br>
<br>
sub myround<br>
{<br>
   my $src = shift;<br>
   printf("%.2f\n", $src);<br>
}<br>
<br>
myround("1.335");<br>
myround("0.335");<br>
<br>
outputs this:<br>
$ perl ./<a href="http://round.pl" target="_blank">round.pl</a><br>
1.33<br>
0.34<br>
<br>
I know why.  It has to do with the well known side effects of using floating<br>
point.<br>
<br>
What I don't know is a graceful way to round.<br>
<br>
I would like to use something that already comes with perl and void adding<br>
any modules.<br>
<br>
--<br>
potter<br>
_______________________________________________<br>
Chicago-talk mailing list<br>
<a href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/chicago-talk" target="_blank">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br>
<br>
_______________________________________________<br>
Chicago-talk mailing list<br>
<a href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/chicago-talk" target="_blank">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br> "Debugging is twice as hard as writing the code in the first place.<br>
   Therefore, if you write the code as cleverly as possible, you are,<br>
   by definition, not smart enough to debug it." -- Brian Kernighan<br><br>