Unless I have misunderstood how round() function works in PHP, I think there is a bug in it:
Example:
Running:
$prod = 201.6 * 0.000275;
echo "No Round..: " . $prod;
echo "With Round: " . round($prod, 2, PHP_ROUND_HALF_DOWN);
PHP Returns:
No Round..: 0.05544
With Round: 0.06
What I expect:
With Round, I expected 0.05 as a result. Is this expected result right?
Enviroment:
PHP 5.3.4 running on Windows 7 64bits with Apache 2.2.17.
Tnks a lot.
That is the correct expected result.
PHP_ROUND_HALF_DOWN
only comes into play when the value is exactly at half, for your case that would be 0.055.
Anything more than 0.055, eg. 0.0550000001, would result in 0.06.
If you prefer to round down, you should try floor()
.
<?php
$prod = 201.6 * 0.000275;
$prod = floor($prod * 100)/100;
echo $prod; // gives 0.05
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With