From 085fafe28d68885bfd829af1828ffbe9bf7fc0a2 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Tue, 7 May 2024 14:06:14 +0200 Subject: [PATCH 1/2] Avoid rounding issues with trick maths --- inc/utils.php | 6 +++++- src/Payment/OrderLines.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/inc/utils.php b/inc/utils.php index 984e54f38..b07868025 100644 --- a/inc/utils.php +++ b/inc/utils.php @@ -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, '.', ''); } diff --git a/src/Payment/OrderLines.php b/src/Payment/OrderLines.php index 440bda158..de67b7c50 100644 --- a/src/Payment/OrderLines.php +++ b/src/Payment/OrderLines.php @@ -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; From e155875aec875afa8085d9160a301bce6c05a318 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Wed, 8 May 2024 10:53:52 +0200 Subject: [PATCH 2/2] Fix CS --- inc/utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/utils.php b/inc/utils.php index b07868025..6a183ffd6 100644 --- a/inc/utils.php +++ b/inc/utils.php @@ -187,8 +187,8 @@ function mollieWooCommerceFormatCurrencyValue($value, $currency) 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 = $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, '.', '');