Skip to content

Commit

Permalink
Avoid rounding issues with trick maths
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed May 7, 2024
1 parent e490014 commit 085fafe
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 085fafe

Please sign in to comment.