Skip to content
Advertisement

Round decimal number to .5 when decimal is below .5 – PHP

Is there any way to round up decimal number to .5 if the decimal value is below or equal to .5 instead of rounding up to whole number?

Ex:

0.12  => 0.5
0.23  => 0.5
12.39 => 12.5...

but when the decimal value is greater than .5 round the number up

0.51   => 1.0
0.60   => 1.0
12.80  => 13.0

Advertisement

Answer

If x is your variable do Ceil($x * 2) / 2 Which will round your number to the nearest whole number or 0.5

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement