Jump to content

Archived

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

phpvolution

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.