Last active
July 13, 2016 04:48
-
-
Save hanafiah/73a6a55907ddc6650d232dba2d824da2 to your computer and use it in GitHub Desktop.
round up issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* problem solved. | |
* thanks | |
**/ | |
$spp002 = 1.70; | |
$bizhubC360 = 1; | |
$InPage = 45; | |
$r = ($spp002 + $bizhubC360) / $InPage; | |
//echo $r. test result shows 0.06 | |
echo '$r = ' . $r; | |
echo PHP_EOL; | |
$r =(float)sprintf('%.11f' ,$r); | |
var_dump($r); | |
echo '$r = ' . $r; | |
echo PHP_EOL; | |
//test1, using variable $r | |
//output shows 0.07, expected output suppose to be 0.06 | |
echo round_up($r,2); | |
echo PHP_EOL; | |
//test2, enter the $r value, 0.06 | |
//output shows 0.06, which is as expected | |
echo round_up(0.06,2); | |
echo PHP_EOL; | |
function round_up($value, $places) | |
{ | |
$mult = pow(10, abs($places)); | |
return $places < 0 ? ceil($value / $mult) * $mult : ceil($value * $mult) / $mult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment