[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: percentage discount not calculating properly



On Wed, 24 Feb 2010, Chris Travers wrote:

On Wed, Feb 24, 2010 at 6:50 PM, Bob Miller <..hidden..> wrote:
your test:
quantity 7, unit price $100.050, discount 15%, extended price $595.29.

my calculator says:
7*100.05*.85=595.2975, so I think the correct answer should be
$595.30...

change the price to $100.0500 and you will get an extended price of $595.30

but that's only because the deducted 15 percent results in a 4 place right of decimal number. If it had resulted in, say, a 6 place one, the only way to cover the case would be to set your product sellprices to 100.050000. More zeros cover more unusual cases.

To me, that seems like something the user really shouldn't have to do, although I guess it's better than the alternative of no options.

However...

Starting on line 307 of IS.pm (1.2.18), the linetotal is rounded to 2 decimal places, no matter what it was before. Could this be changed to use $decimalplaces without causing problems down stream? That would get my stage 2 from a previous message.

$decimalplaces, for that matter, is determined from the cellprice, in lines 293-295.
Could we draw that from a default setting instead?

Or, for users like me and apparently Bob's customer who want this kind of accuracy, can we change the default set in those lines to something like 10, without causing problems? Even changing it to 4, should do the same thing as updating the sellprice on all products to pad to 4 zeros to the right, shouldn't it? (What is the maximum precision?)

This is Easier for me than for Bob's client, I imagine.

If any of these do cause problems, I'm thinking they could be solved by changing line 345:

$form->{total} += $linetotal;

To:

$form->{total} += $form->round_amount($linetotal, 2);

although I'd rather do that rounding after all the items are iterated, I just haven't found where yet. (Line 605 maybe?)


As an aside, I've noticed that in perl native, without the use of math::BigFloat:

1.05 with a 91% discount (1.05 - (1.05 * 0.91)) = 0.0944999999999999
It should be 0.0945.

(perl -e 'print 1.05 - (1.05 * 0.91), "\n";')

I'm sure BigFloat handles that correctly in the Form.pm functions, but does it also handle it in the case of IS.pm, and the code which starts on line 297? (1.2.18)

my $discount = $form->round_amount($sellprice *
$form->parse_amount( $myconfig, $form->{"discount_$i"} ) /
100, $decimalplaces);

Luke