Sergio
Sergio

Reputation: 1239

PHP round decimals

Is it possible to round a decimals to the nearest .5 with PHP like this:

number | round
----------------
 1.29  |  1.50
 2.03  |  2.00
 1.43  |  1.50
 1.13  |  1.00
11.38  | 11.50

I tried with:

$rnd= round($number,2);

but I get decimals like the one in the column "number" above.

Upvotes: 7

Views: 8230

Answers (2)

john
john

Reputation: 5

You don't want write any function for this php round function already have the option, in mode is a third argument of round function. for more reference PHP Round Function

Upvotes: -2

Artefacto
Artefacto

Reputation: 97825

function round_to_nearest_half($number) {
    return round($number * 2) / 2;
}

Upvotes: 17

Related Questions