1

I was wondering what php function could help me to do what I am looking for. I want to round number to the half up.

For example:

1.16 => 1.5,
0.5 => 0.5
1 => 1

I wanted to use round function like this :

round(1.16, 0.5, PHP_ROUND_HALF_UP)

But it returns 1 instead of 1.5. I probably dont understand well what the function is doing.

yivi
  • 42,438
  • 18
  • 116
  • 138
mlwacosmos
  • 4,391
  • 16
  • 66
  • 114
  • Also, you can't round to _half a digit_. `PHP_ROUND_HALF_UP` says that you will round to the higher number if you are exactly halfway between two candidates, not that it will round to halves. You need a bit of maths for that one, not just a function. – Amadan Nov 20 '19 at 10:45
  • Needs details or clarity. – nice_dev Nov 20 '19 at 10:46
  • `I want to round number to the half up.` You probably meant to the nearest half up. – nice_dev Nov 20 '19 at 10:56

1 Answers1

4

Here is one option. We can multiply the input number by 2, then take the ceiling, and finally divide by two, e.g.

$input = 1.16;
$input_rounded = ceil(2*$input) / 2;
echo $input_rounded;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360