Skip to content

Commit

Permalink
Merge pull request #903 from mollie/fix/PIWOO-461_rounding
Browse files Browse the repository at this point in the history
Avoid rounding issues
  • Loading branch information
mmaymo authored May 13, 2024
2 parents 46737da + e155875 commit 9a923b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion inc/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ function mollieWooCommerceFormatCurrencyValue($value, $currency)
if (in_array($currency, $currenciesWithNoDecimals)) {
return number_format($value, 0, '.', '');
}

// trying to avoid floating point issues
$value = $value * 1000;
$value = (int) $value / 1000; //drop the last decimal after the third
$value = round($value, 3);
$value = round($value, 2, PHP_ROUND_HALF_DOWN); //round down, as seems woo like it :)
return number_format($value, 2, '.', '');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Payment/OrderLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ private function get_shipping_vat_rate()
{
$shipping_vat_rate = 0;
if (WC()->cart->shipping_tax_total > 0) {
$shipping_vat_rate = round(WC()->cart->shipping_tax_total / WC()->cart->shipping_total, 2) * 100;
$shipping_vat_rate = round(WC()->cart->shipping_tax_total / WC()->cart->shipping_total, 4) * 100;
}

return $shipping_vat_rate;
Expand Down

0 comments on commit 9a923b5

Please sign in to comment.