1

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;
Tonechas
  • 13,398
  • 16
  • 46
  • 80
Nicolas
  • 4,526
  • 17
  • 50
  • 87
  • 1
    Similar question asked but with more options for rounding, take a read @ http://stackoverflow.com/questions/14903379/rounding-to-nearest-fraction-half-quarter-etc – Wolfeh Jun 16 '16 at 12:31

1 Answers1

6

Use round() function:

$x = round($x * 2) / 2;
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
  • 1
    Multiplications are easier on the CPU than divisions, so this would be better: `$x = 0.5 * round($x * 2);`. Would the PHP parser be clever enough to do this conversion? – KIKO Software Jun 16 '16 at 12:34
  • He still rounds off 2.5 to 3 for some reason? – Nicolas Jun 16 '16 at 12:46