phpvolution Posted June 14, 2006 Share Posted June 14, 2006 I've looked for a while to find an answer on this and haven't come up with anything.I have a number, say 6.7I want it to round in increments of .5, and to the nearest. I'm trying to make a php version of the formula I used in excel, which was the floor() function.. only in excel I used: =FLOOR(SUM(I4:L4)/4,0.5)So 6.7 should be 6.5, 6.75 should still be 6.5, 6.76 should be 7. And so on.Any ideas on how I can do this? Link to comment https://forums.phpfreaks.com/topic/11958-rounding-to-the-nearest-5/ Share on other sites More sharing options...
joquius Posted June 14, 2006 Share Posted June 14, 2006 sure take$string * 2and then round to nearest and then divide it by 2ex:6.4 * 2 = 12.8 = 13 = 6.56.23 = 12.46 = 12 = 6 Link to comment https://forums.phpfreaks.com/topic/11958-rounding-to-the-nearest-5/#findComment-45442 Share on other sites More sharing options...
phpvolution Posted June 14, 2006 Author Share Posted June 14, 2006 Thanks, my brain doesn't seem to be functioning![code]$x = floor($x*2)/2;[/code]voila. Link to comment https://forums.phpfreaks.com/topic/11958-rounding-to-the-nearest-5/#findComment-45444 Share on other sites More sharing options...
joquius Posted June 14, 2006 Share Posted June 14, 2006 you will need to use round if you want 7 for 6.8 and not 6.5 Link to comment https://forums.phpfreaks.com/topic/11958-rounding-to-the-nearest-5/#findComment-45445 Share on other sites More sharing options...
phpvolution Posted June 14, 2006 Author Share Posted June 14, 2006 Thanks for pointing that out,6.75 was being rounded up instead of down also, so I went with an If statement to handle that.[code]if(substr($x,1) > .75) {$y = round($x*2)/2;}else {$y = floor($x*2)/2;}[/code]Seems to work. thanks. Link to comment https://forums.phpfreaks.com/topic/11958-rounding-to-the-nearest-5/#findComment-45731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.