Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round a number at 0.5

Tags:

php

I try to round a number at 0.5

This is my examples :

5.1 > 5
2.4 > 2.5
1 > 1
6.8 > 7
6.2 > 6.5

I try with :

<?php
round((4.4 * 2) / 2)
?>

But :

5.4 > 5
4.7 > 5

instead of

5.4 > 5.5
4.7 > 4.5

Any idea ?

like image 245
Justine MARTIN Avatar asked Dec 06 '25 09:12

Justine MARTIN


1 Answers

Why not just double the value, round on a full number and then devide by 2:

$res = round($value*2) / 2

That should do the trick.

like image 103
Stefan Dochow Avatar answered Dec 08 '25 23:12

Stefan Dochow