I am looking for the best way to str_pad floats in php.
The problem I am having is that str_pad takes into account the string length but with a float there is the decimal point. If I want a number padded with zeros this becomes a little bit of a nightmare.
For example if I need the number to always have at least 6 numbers after the decimal point I cannot do it with str_pad as the results change.
$i = 1.0;
echo str_pad($i, 8, '0'); // 1.000000
$i = 10.5
echo str_pad($i, 8, '0'); // 10.50000 ## ONLY PADDED WITH 5 NUMBERS AFTER DECIMAL
I know I can explode then str_pad and do it that way but just wondered if there was a php built in function for this or any better ways.
Use
number_formatYou can also automatically add separators and spaces etc
Documentation