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!
3
Answers
You can combine
round()
andnumber_format()
:For example:
I’d use
sptrintf
to format the number to 1 decimal, then append a0
behind it:Test cases:
Try it online!
As string, since trailing
0
on a float are removed by PHPI don’t think there is a native php function already doing what you want. We will need to do it ourself. First by finding the depth of the decimal part, then by rounding depending on depth.
You can print your number with the precision you want with
number_format
then.