Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 5 of 5
1.6.0.0 price rounding issues, wrong price displayed on grid and product view
 
wardown
Jr. Member
 
Total Posts:  4
Joined:  2010-01-28
 

rounding to 4 digits is not correct… it makes differencies in paypal and you will see suspected fraud in it

 
Magento Community Magento Community
Magento Community
Magento Community
 
rochi
Jr. Member
 
Total Posts:  7
Joined:  2010-01-11
 

The only solution that works:
The Shop have to calculate the tax item for item to prevent rounding issues with row totals.
Go to System->Configuration->Taxes and change calculation method to “Unit Price”

Don’t modify your code.

If you have still issues with tax calculation, myabe this helps:
Change the code in Mage_Tax_Helper_Data from

protected function _calculatePrice($price$percent$type)
    
{
        $calculator 
Mage::getSingleton('tax/calculation');
        if (
$type{
            $taxAmount 
$calculator->calcTaxAmount($price$percentfalse);
            return 
$price $taxAmount;
        
else {
            $taxAmount 
$calculator->calcTaxAmount($price$percenttrue);
            return 
$price $taxAmount;
        
}
    }
To
protected function _calculatePrice($price$percent$type)
    
{
        $calculator 
Mage::getSingleton('tax/calculation');
        if (
$type{
            $taxAmount 
$calculator->calcTaxAmount($price$percentfalsefalse);
            return 
$price $taxAmount;
        
else {
            $taxAmount 
$calculator->calcTaxAmount($price$percenttruefalse);
            return 
$price $taxAmount;
        
}
    }

This fix comes from Magento 1.7

 
Magento Community Magento Community
Magento Community
Magento Community
 
wardown
Jr. Member
 
Total Posts:  4
Joined:  2010-01-28
 

problem appeared after update to 1.7.0.2 from 1.6.2.0.... this solution is not working… there is incorrect tax calculation… makes differencies between excluded tax price and included tax price in order and invoice....

 
Magento Community Magento Community
Magento Community
Magento Community
 
kyllaz
Jr. Member
 
Total Posts:  7
Joined:  2011-06-19
 
Spez86 - 22 November 2012 07:39 AM

Hi all.

I’ve managed to sort of fix this on my setup 1.7.0.2 as below:

1. All my prices all include tax
2. I apply tax after discounts and discount on prices including tax.
3. I’ve set Tax Calculation based on Row Total.

I’ve copied the file: app/code/core/Mage/Core/Model/Store.php to the app/code/local with the same directory structure so I don’t override core files and I’ve simply done what the other says above which is to change:

return round($price2);
to
return round($price4);

However, exactly how it rounds seems to still be a mystery to me as it seems to be rounding 0.8333 pence up to 0.84 pence.

Thanks Spez, works perfect on 1.7.0.2

 
Magento Community Magento Community
Magento Community
Magento Community
 
comicbuchleser
Newbie
 
Total Posts:  0
Joined:  2012-07-03
 

im using magento 1.7.2 and have quite complex bundle products.

the fix

return round($price4);

did work for me, but i had (like everybody) issues with the payment services - especially with discounts and when canceling items from an order in the backend

so i switched in System->Configuration->Taxes the calculation method to “Unit Price” but the total was totaly messed up afterwards.
for me it worked to override the file /magento/app/code/local/Mage/Tax/Model/Sales/Total/Quote/Tax.php
line 383 to 391 i changed

$unitTax $this->_calculator->calcTaxAmount($price$rate$inclTax);
$discountRate = ($unitTax/$price) * 100;
$unitTaxDiscount $this->_calculator->calcTaxAmount($discountAmount$discountRate$inclTaxfalse);
$unitTax max($unitTax $unitTaxDiscount0);
$baseUnitTax $this->_calculator->calcTaxAmount($basePrice$rate$inclTax);
$baseDiscountRate = ($baseUnitTax/$basePrice) * 100;
$baseUnitTaxDiscount $this->_calculator
                    
->calcTaxAmount($baseDiscountAmount$baseDiscountRate$inclTaxfalse);
$baseUnitTax max($baseUnitTax $baseUnitTaxDiscount0);
to
$unitTax $this->_calculator->calcTaxAmount($price$rate$inclTax);
$discountRate = ($price==0) ? : ($unitTax/$price) * 100;
$unitTaxDiscount $this->_calculator->calcTaxAmount($discountAmount$rate$inclTaxfalse);
$unitTax max($unitTax $unitTaxDiscount0);
$baseUnitTax $this->_calculator->calcTaxAmount($basePrice$rate$inclTax);
$baseDiscountRate =  ($basePrice==0) ? : ($baseUnitTax/$basePrice) * 100;
$baseUnitTaxDiscount $this->_calculator
                    
->calcTaxAmount($baseDiscountAmount$rate$inclTaxfalse);
$baseUnitTax max($baseUnitTax $baseUnitTaxDiscount0);

it seems that magento somehow changed the tax-rate of the discount product before calling calcTaxAmount. ($discountRate was something like 15.934843 or so.) after that fix it worked fine for me. even when using credit memos (sending part of an order back to the customer).

to get paypal working (status was changed to \’fraud suspected\’ instead of \’processing\’), i used the fix from another forum and modified /magento/app/code/local/Mage/Sales/Model/Order/Payment.php

protected function _isCaptureFinal($amountToCapture)
    
{
        $amountToCapture 
$this->_formatAmount($amountToCapturetrue);
        
$orderGrandTotal $this->_formatAmount($this->getOrder()->getBaseGrandTotal(), true);

        
//Original line if ($orderGrandTotal == $this->_formatAmount($this->getBaseAmountPaid(), true) + $amountToCapture) {
        
if (abs($orderGrandTotal - ($this->_formatAmount($this->getBaseAmountPaid(), true) + $amountToCapture)) <= 0.01 {
            
if (false !== $this->getShouldCloseParentTransaction()) {
                $this
->setShouldCloseParentTransaction(true);
            
}
            
return true;
        
}
        
return false;
    
}

all that does, is tell magento that if amount paypal calculated (not whats payed to by the customer.. that is correct) is of by 0.01 euro it will still be a valid transaction (no fraud).

So i had to override 3 core files (copied them to /local/Mage first of course) but its working now (for all that i can tell).

since it took me the better part of day to find that out, i thought it might be usefull for somebody.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

Hi all,

Sure enough rounding to 4 decimal places causes Paypal to reject the request.

I haven’t tried the fix posted above but really and truly this shouldn’t be happening. The only way around it as I can see as a fix which doesn’t touch any code is to find a price which has an EX-VAT price of only 3 decimal places as this will round to 2 as per Magento. Frustating as I have lots of prices to change but I can’t see any way around this until it gets introduced as an actual fix or option within Magento.

I’ve actually spoken to a couple of fellow UK Magento users and they’re saying just that: use whole pounds or at least prices that don’t end up with EX-VAT prices with 6 decimal places…

Does anybody know of a way to turn of PayPal checking line items and causing this mismatch and just simply receiving the total that Magento sends it?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

*Update*

Right. I think I tried every single solution on here to get this to work but it never did 100% of the time. However, I seem to have stress-tested my system as much as I possibly can (within reason) and I can’t seem to reproduce the rounding error anymore. *Fingers crossed*.

It seems that rounding tax to 4 decimal places solves the problem if you’re including tax/vat in your prices & shipping, applying tax AFTER discount and have calculation method set to ‘Row Total’. For percentage discounts I have ‘tweaked’ shall we say, the percentage so that it rounds the discount up itself, resulting in the correct amount of tax/vat and grand total (I ended up with an extra penny/cent on the grand total even though subtotal, discount and tax/vat all calculated correctly!)

I’ll post back when I’ve done some more stress testing (and yes, I mean stress - I have made calculations purposely difficult for Magento with lots of prices ending in 9’s 7’s and 5’s all inclusive of tax/vat to see if I can reproduce errors)

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

With using the 4 decimal place hack, 1.7 store.

I then used the following hack of NVP to get Paypal Express working for a UK store that also used percentage based discounts.

http://www.magentocommerce.com/boards/viewthread/291325/

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

I can confirm Elfling’s suggested fix works for me on 1.7.0.2 with tax calculation set to Total as well as my earlier post too. Not sure which is best.

 
Magento Community Magento Community
Magento Community
Magento Community
 
InfoDesires
Jr. Member
 
Avatar
Total Posts:  12
Joined:  2011-07-31
UK
 

Steps to follow:
1. Move /app/code/core/Mage/Tax/Model/Calculation.php to /app/code/local/Mage/Tax/Model/Calculation.php
2. Edit Calculation.php file and find calcTaxAmount function in it (Around line no: 466)
3. Find following code in that same function

if ($priceIncludeTax{
    $amount 
$price*(1-1/(1+$taxRate));
else {
    $amount 
$price*$taxRate;
}

4. Just after it, add following line of code

$amount $this->truncate($amount2);
5. And test implementation at front-end.

This is tested and working for magento 1.7.0.0.

 
Magento Community Magento Community
Magento Community
Magento Community
 
superforma
Jr. Member
 
Total Posts:  1
Joined:  2013-04-21
 

Doesn’t seem to work.

Only way I seem to be able to do it, admin show products exc vat, front end show products inc vat.

When entering £20.83 into the admin product price I get £25.00 on the front end (with UK 20% VAT)

If I enter £20.825 in the admin section then save, it shows as rounded up to £20.83in admin but on the front end it shows as £24.99

Random but seems to work

 
Magento Community Magento Community
Magento Community
Magento Community
 
loeffel
Guru
 
Avatar
Total Posts:  427
Joined:  2009-02-03
 

Has anyone tried this fix:
http://www.pa-s.de/php/codeschnipsel-Magento-1.7.0.2---Rundungsfehler-Fix-67.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
adyballa
Jr. Member
 
Total Posts:  5
Joined:  2012-08-14
 

hello,

I’m kind frustrated by the discussion and the way of the solutions. I found no open ears by now.

Now my opinion:
The problem is not solvable, if you are calculating with rounded prices.
There is a general relationship between the factors of the multiplications and the precision.
example:
You enter your price incl. taxes in a thirdParty-system. The thirdParty is sending you a rounded price excluding taxes.
Now you cannot guarantee a price if you dont fullfill some constraints.
If you round to 4 fractional digits, the taxes must be less than 90900%.
If you get back 2 fractional digits, you cannot ensure a price at all!

another example:
You are calculating a tax from a price and then you want to recalculate the price. Assume we have a precision of 4 digits, now the tax-rate must be over 1 percent.
f.e.:
Total: 15,15 tax-rate: 0,3% => tax 0,04545 => rounded 0,0455 tax: 0,0455 => total: 15,17

conclusion:
a) If you are developing in magento with prices, always take the original price. Do not calculate with interim results.
(Enter prices without taxes)
b) Calculate the constraints and communicate them

thank you.

If you are interested and if you have some doubts, please visit
http://magento.stackexchange.com/questions/829/what-is-the-status-of-rounding-issues-in-1-7/8097#8097
and falsify my argumentation.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 5 of 5