#### Archived

This topic is now archived and is closed to further replies.

# Rounding to the nearest .5

## Recommended Posts

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.7

I 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?

##### Share on other sites
sure take
\$string * 2
and then round to nearest and then divide it by 2
ex:
6.4 * 2 = 12.8 = 13 = 6.5
6.23 = 12.46 = 12 = 6

##### Share on other sites
Thanks, my brain doesn't seem to be functioning!

[code]
\$x = floor(\$x*2)/2;
[/code]
voila.

##### Share on other sites
you will need to use round if you want 7 for 6.8 and not 6.5

##### Share on other sites
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.