qwertz qwertz - 1 month ago 13
PHP Question

PHP zerofill number without losing decimals

how can I zerofill a number without loosing decimals?
I tried using sprintf but it deleted all my decimals, what I don't want.

I have the number 12.2 and I want it to look like 012.2, but sprintf gives me 012 and removes the decimals. The function I'm using is:

sprintf('%02u', $number);

Answer

You have a good explanation of the sprintf format in the PHP manual, which applies to printf as well.

printf('%05.1f', 12.2);

Output:

012.2
Comments