5

I have a store that is configured to store product prices as inclusive of tax. In the tax configuration, I have 'Tax Calculation Method Based On' set to Total.

On some orders that have discounts, I have noticed strange rounding issues. For example, an order that contains 3 lines, totalling £290, with a discount of £65, has one of the order lines with a line total of £55.01, instead of £55 (which is the correct product price) - making the order total £225.01, rather than £225.

What is the cause of the above, and can changing the 'Tax Calculation Method Based On' setting influence this?

2 Answers 2

2

This is probably related to the famous magento rounding bug.

As a solution you can copy /app/code/core/Mage/Core/Model/Store.php file to /app/code/local/Mage/Core/Model/Store.php and modify the roundPrice() method to round with 4 digits:

public function roundPrice($price)
{
    return round($price, 4);
}
1
  • 1
    it seems crazy this bug exists, in latest 1.9 release?! I will look at implementing your suggested fix, thank you.
    – BrynJ
    Commented Jan 18, 2016 at 10:37
0

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);
}

P.P.S.

This answer is a duplicate. I am adding here answer because I do not have enough reputation to mark this question as a duplicate.

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.