c_shelswell Posted February 26, 2007 Share Posted February 26, 2007 Hi i have a function that i got off the php manual site. It rounds up figures to the nearest 2 decimals for me but an odd thing happened last night. Basically i fed it 1.1 and it gave me 1.11 here's my code function roundUp ($value, $places=2) { if ($places < 0) { $places = 0; } $mult = pow(10, $places); return ceil($value * $mult) / $mult; } any help would be great cheers Link to comment https://forums.phpfreaks.com/topic/40145-need-a-bit-of-help-with-a-rounding-up-function/ Share on other sites More sharing options...
Threepwood Posted February 26, 2007 Share Posted February 26, 2007 Hi, try use round($number,$decimals) http://it.php.net/round For example: function roundUp ($value, $places=2) { if ($places < 0) { $places = 0; } $mult = pow(10, $places); $x=ceil($value * $mult) / $mult; return round($x,2); } Link to comment https://forums.phpfreaks.com/topic/40145-need-a-bit-of-help-with-a-rounding-up-function/#findComment-194237 Share on other sites More sharing options...
Archadian Posted February 26, 2007 Share Posted February 26, 2007 or you can try number_format() which is alot easier: http://us2.php.net/manual/en/function.number-format.php Link to comment https://forums.phpfreaks.com/topic/40145-need-a-bit-of-help-with-a-rounding-up-function/#findComment-194238 Share on other sites More sharing options...
c_shelswell Posted February 26, 2007 Author Share Posted February 26, 2007 hi three that seems to still return 1.11 sadly i can't use number format as i need to round it up. i.e. 1.911 should be rounded to 1.92 so number format won't work. Link to comment https://forums.phpfreaks.com/topic/40145-need-a-bit-of-help-with-a-rounding-up-function/#findComment-194268 Share on other sites More sharing options...
Hooker Posted February 26, 2007 Share Posted February 26, 2007 Try this, it should let you round up or down depending on what you want <?php function rounding($no,$direction) { $skip=0; if(is_float($no) and $direction = 1) { $exploded = explode(".",$no); $nrr = $exploded[0]+1; $skip=1; } if(is_float($no) and $direction = 0) { $exploded = explode(".",$no); $nrr = $exploded[0]; $skip=1; } if(!is_float($no) and $skip ==1) { $nrr = $nrr; } else { $nrr = floor($nrr); } return $nrr; } ?> Use it like this: echo rounding(1.6,0); Enter the number first followed bya boolen, 1 to round up, 0 to round down. Always a nice little function to have lying around Link to comment https://forums.phpfreaks.com/topic/40145-need-a-bit-of-help-with-a-rounding-up-function/#findComment-194276 Share on other sites More sharing options...
c_shelswell Posted February 26, 2007 Author Share Posted February 26, 2007 Cool thanks i also just found this which seems to work too. function roundUp( $value, $dec = 2 ) { $value += 0.00; $unit = floor( $value * pow( 10, $dec + 1 ) ) / 10; $round = ceil( $unit ); return $round / pow( 10, $dec ); } i guess you could replace the ceil to floor depening on if you wanted to round up or down. Cheers Link to comment https://forums.phpfreaks.com/topic/40145-need-a-bit-of-help-with-a-rounding-up-function/#findComment-194288 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.