Search code examples
phprounding

Rounding last decimal place to 0?


I tried round, ceil and floor, but they do not seem to do what I want (or I dont see the wood for the trees :-))

This is what I want:

1.44 --> 1.40
1.23 --> 1.20
3.50 --> 4.00
2.48 --> 2.50
...

I did not find a way to achieve that. Should be possible?

Thanks!


Solution

  • You can combine round() and number_format():

    For example:

    $number = 1.44;
    number_format(round($number, 1), 2, '.', ''); // 1.40