pacchiee Posted July 2, 2009 Share Posted July 2, 2009 Hello, I want to round off an integer to end with 5 or 0 with PHP. For example: 21.4 should be rounded to 20 23.8 should be rounded to 25 26.4 should be rounded to 25 28.1 should be rounded to 30 and so on.. Please help. Thanks in Advance. Link to comment https://forums.phpfreaks.com/topic/164526-solved-rounding-an-integer/ Share on other sites More sharing options...
nbarone Posted July 2, 2009 Share Posted July 2, 2009 <?php function roundnum ($num, $nearest) { $ret = 0; $mod = $num % $nearest; if ($mod >= 0) $ret = ( $mod > ( $nearest / 2)) ? $num + ( $nearest - $mod) : $num - $mod; else $ret = ( $mod > (-$nearest / 2)) ? $num - $mod : $num + ( -$nearest - $mod); return $ret; } echo roundnum (1234, 15); // round to the nearest 15 echo roundnum (1234, 5); // round to the nearest 5 ?> http://www.markblah.com/2008/05/11/php-round-number-to-nearest-x/ Link to comment https://forums.phpfreaks.com/topic/164526-solved-rounding-an-integer/#findComment-867797 Share on other sites More sharing options...
pacchiee Posted July 2, 2009 Author Share Posted July 2, 2009 Hello nbarone, Thanks. That function rounds an integer to the nearest specified SINGLE number. But I want to round an integer to its nearest 5 or 10 BOTH. Link to comment https://forums.phpfreaks.com/topic/164526-solved-rounding-an-integer/#findComment-867842 Share on other sites More sharing options...
nbarone Posted July 2, 2009 Share Posted July 2, 2009 Quote Hello nbarone, Thanks. That function rounds an integer to the nearest specified SINGLE number. But I want to round an integer to its nearest 5 or 10 BOTH. Sorry, I guess I don't quite understand what you're trying to accomplish, this function rounds to the nearest 5. Link to comment https://forums.phpfreaks.com/topic/164526-solved-rounding-an-integer/#findComment-867934 Share on other sites More sharing options...
nbarone Posted July 2, 2009 Share Posted July 2, 2009 Maybe this is what you were looking for....it takes decimal into account...prolly not the most efficient way, but it works: <?php function roundnum ($num, $nearest) { $ret = 0; $mod = $num % $nearest; if ($mod >= 0) $ret = ( $mod > ( $nearest / 2)) ? $num + ( $nearest - $mod) : $num - $mod; else $ret = ( $mod > (-$nearest / 2)) ? $num - $mod : $num + ( -$nearest - $mod); $dec_check = explode(".",$ret); if($dec_check[1]){ $tenth = $dec_check[1]; if($tenth > 4){ $ret = $ret+$nearest; } } return floor($ret); } echo roundnum (17.5, 5); // = 20 echo roundnum (17.4, 5); // = 15 ?> Link to comment https://forums.phpfreaks.com/topic/164526-solved-rounding-an-integer/#findComment-867941 Share on other sites More sharing options...
pacchiee Posted July 3, 2009 Author Share Posted July 3, 2009 Hello nbarone, Thanks so much for you help. I got it working with the above function. Cheers! Link to comment https://forums.phpfreaks.com/topic/164526-solved-rounding-an-integer/#findComment-868103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.