[Chicago-talk] (no subject)

Michael Potter michael at potter.name
Sat Apr 28 06:39:36 PDT 2012


I ended up using the Math::Decimal module:

my $rounded = dec_round("NEAR_AWZ", $unrounded, "0.01");

Limited testing indicated that that matched the rounding of excel.

AWZ means away from zero.



On Sat, Apr 28, 2012 at 12:29 AM, imran javaid <imranjj at gmail.com> wrote:
> doesn't work for negative number, btw. need to check the sign in that case.
>
>
> On Fri, Apr 27, 2012 at 11:26 PM, imran javaid <imranjj at gmail.com> wrote:
>>
>> 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
>>
>>
>
>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk



-- 
Michael Potter
  Tapp Solutions, LLC
  Replatform Technologies, LLC
+1 770 815 6142  ** Atlanta ** michael at potter.name  **
www.linkedin.com/in/michaelpotter


More information about the Chicago-talk mailing list