2

Currently I am working with PHP 5.3.29 and the round() function in PHP_ROUND_HALF_DOWN mode to round a specific number.

Here is my code:

function getBetragrabatt($betrag, $rabatt, $ratezahl = false){
    if(!empty($rabatt)){
        $rabatt = str_replace(array(",", "."), array("", ""), $rabatt);
        if($ratezahl >=1)
            $abschlag = ($rabatt / $ratezahl);
        else
            $abschlag = $rabatt;
        $abschlag = floor($abschlag / 100) . '.' . ($abschlag % 100);
        $rabattbetrag = ($betrag - round($abschlag, 0, PHP_ROUND_HALF_DOWN));
    }
    return $rabattbetrag;
}

$rateRabatt = getBetragrabatt(119.95, 100, 18);

echo $rateRabatt;

If I use the code on sandbox (click here) everything works as expected and I get 119.95 as result.

If I use the same code within my IDE or upload it to my server, I get 119.40 as result.

What am I doing wrong? Hope you can help me..

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Codehan25
  • 2,704
  • 10
  • 47
  • 94
  • This doesn't really fix your problem but you really want to switch to a newer version of PHP where I, dare say, this will be fixed (like in the sandbox). – H2ONOCK Jan 02 '17 at 14:05
  • @H2ONOCK I would like to do that too, but I am currently dependent on this version :( – Codehan25 Jan 02 '17 at 14:09
  • Assuming that PHP version is in fact your issue, you'll need to write your own function that deals with rounding rather than using PHP's native one. Some of the comments on the php website may help... http://php.net/manual/en/function.round.php – H2ONOCK Jan 02 '17 at 14:22
  • @H2ONOCK I'm using the version 5.3.29 and according to the documentary the round function with PHP_ROUND_HALF_DOWN mode is available since Version 5.3. Thanks anyway :) – Codehan25 Jan 02 '17 at 14:37
  • Hi, I'm aware of that but I'm wondering if there was a bug in that version, I can't remember that far back. I'd be interested to hear how you get/got around the issue. – H2ONOCK Jan 04 '17 at 11:08
  • @H2ONOCK Unfortunately, I have not yet found a solution. I am "avoiding" the problem by using the floor function: round (floor ($var)). This approach is, of course, not perfect. – Codehan25 Jan 05 '17 at 07:17

0 Answers0