Nicolas
Nicolas

Reputation: 4756

PHP - Rounding of numbers to nearest multiple of 0.5

Just had a really simple question.

I'm making a review system and I want the scores to be rounded off to the nearest multiple of 0.5

So what I mean is:

0 / 0.5 / 1 / 1.5 / 2 / 2.5 /....

What's the best way of doing this?

I currently have this code but this isn't working

$score = ($food + $staff + $value + $atmosphere) / 4;
$score = ceil($score * 2) / 2;

Upvotes: 1

Views: 3507

Answers (1)

Dhara Parmar
Dhara Parmar

Reputation: 8101

Use round() function:

$x = round($x * 2) / 2;

Upvotes: 6

Related Questions