3

In Magento 1.7 there is a well known rounding error issue, which I thought I have solved using the fix suggested for example here

However, ever since I have applied the fix, there is now a very strange new calculation error, not for taxes, but already for the items in cart. It only happens when the amount of an item in the cart is more than one.

Example: 5 times an item with a price of 1.18 € each equals 5.88 € in cart and not 5.90 € as supposed.

Once I disable the rounding error fix again, price calculation for the articles works again, bit than tax is being calculated wrong.

Any idea how to fox this?

3

1 Answer 1

1

Info

Round price in Magento based on previous rounding operation delta.

app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php:1392 app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php:719

protected function _deltaRound($price, $rate, $direction, $type = 'regular')
{
    if ($price) {
        $rate = (string)$rate;
        $type = $type . $direction;
        // initialize the delta to a small number to avoid non-deterministic behavior with rounding of 0.5
        $delta = isset($this->_roundingDeltas[$type][$rate]) ? $this->_roundingDeltas[$type][$rate] : 0.000001;
        $price += $delta;
        $this->_roundingDeltas[$type][$rate] = $price - $this->_calculator->round($price);
        $price = $this->_calculator->round($price);
    }
    return $price;
}

Sometimes this can cause an error due to the high delta calculation error ($this->_calculator->round($price)). For example, for this reason, some prices can vary in the range of ±1 cent.

Solution

To avoid this, you need to improve the accuracy of the delta calculation.

Change

$this->_roundingDeltas[$type][$rate] = $price - $this->_calculator->round($price);

to

$this->_roundingDeltas[$type][$rate] = $price - round($price, 4);

Changes need to be made in both files:

app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php:1392 app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php:719

Don't modify or hack core files! Do a rewrite!

The solution was tested on different versions of Magento 1.9.x, but maybe this will work in earlier versions.

P.S.

Change roundPrice function, as shown below, can solve the rounding error problem, but it can cause others (for example, some platforms require rounding up to 2 decimal places).

app/code/core/Mage/Core/Model/Store.php:995

public function roundPrice($price)
{
    return round($price, 4);
}
4
  • this is not working for me, Kindly try with a product $95.00 and Shipping $0.00, the product amount is Incl. of tax Tax rate is 7%. I calculated tax on Incl. price and based on "Row Total"
    – Yogita
    Commented May 17, 2018 at 18:09
  • @Yogita, what are the prices before and after the changes? Both methods of solving the problem were tested? What version of Magento? Are there any extensions for rounding?
    – Victor S.
    Commented May 18, 2018 at 5:32
  • I have done this: magento.stackexchange.com/questions/225168/… and Using Magento1.9.2.4, My product is Incl. of tax and having 7% tax rate, Product price $95.00
    – Yogita
    Commented May 18, 2018 at 5:34
  • @Yogita, I answered here.
    – Victor S.
    Commented May 18, 2018 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.