From 79d5bc9156cd66c39d8ce95e22564c726efbbaeb Mon Sep 17 00:00:00 2001 From: objecttothis <17935339+objecttothis@users.noreply.github.com> Date: Tue, 19 Sep 2017 16:28:40 +0400 Subject: [PATCH] Correcting Round-up and Round-down bug Former code ignored precision and just rounded up or down to the nearest integer. This code rounds up or down to the precision in cash decimals. --- application/models/enums/Rounding_mode.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/application/models/enums/Rounding_mode.php b/application/models/enums/Rounding_mode.php index 812845a26..39a2b9d93 100644 --- a/application/models/enums/Rounding_mode.php +++ b/application/models/enums/Rounding_mode.php @@ -52,13 +52,13 @@ class Rounding_mode { if($rounding_mode == Rounding_mode::ROUND_UP) { - $fig = (int) str_pad('1', $decimals, '0'); - $rounded_total = (ceil($amount * $fig) / $fig); + $fig = pow(10,$decimals); + $rounded_total = (ceil($fig*$amount) + ceil($fig*$amount - ceil($fig*$amount)))/$fig; } elseif($rounding_mode == Rounding_mode::ROUND_DOWN) { - $fig = (int) str_pad('1', $decimals, '0'); - $rounded_total = (floor($amount * $fig) / $fig); + $fig = pow(10,$decimals); + $rounded_total = (floor($fig*$amount) + floor($fig*$amount - floor($fig*$amount)))/$fig; } elseif($rounding_mode == Rounding_mode::HALF_FIVE) { @@ -71,4 +71,4 @@ class Rounding_mode return $rounded_total; } -} \ No newline at end of file +}