fri3ndly Posted July 1, 2008 Share Posted July 1, 2008 Hello I have a star rating system which will look up ratings that will be '2.3', '0.7' etc I am trying to round up to the nearest half number as I have half stars to show if this is the case... so... 2.3 > 2.5, 0.7 > 1 etc etc Please can someone help as round or ceil arent helping Link to comment https://forums.phpfreaks.com/topic/112772-solved-round-to-05-15-25-etc/ Share on other sites More sharing options...
lemmin Posted July 1, 2008 Share Posted July 1, 2008 <?php $rating=2.4; $rounded = round($rating*2)/2; ?> This code would round 0.7 down because it is less than halfway to 1 from 0.5. It seems to me that you would want to round .6 and .7 down and .8 and .9 up, but if you really want to round .7 up you can just check for it like this: <?php $rating=2.7; if (($rating*10)%10 == 7) $rounded = $rating+0.3; else $rounded = round($rating*2)/2; ?> Link to comment https://forums.phpfreaks.com/topic/112772-solved-round-to-05-15-25-etc/#findComment-579236 Share on other sites More sharing options...
fri3ndly Posted July 1, 2008 Author Share Posted July 1, 2008 Thanks lemmin, the first code is exactly what I mean. I probably didnt explin very well. Perfect! Link to comment https://forums.phpfreaks.com/topic/112772-solved-round-to-05-15-25-etc/#findComment-579240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.