i need to arrange a number to a format like this xxxx.xxxx
. But I get as input something like this (-/+)xx.xxxxxxxxx
.
If the number is x.xx
it should get the above mentioned format by adding 0s
to end and front.
please guide me on the correct way to get this done!
You can use explode
& str_pad
-
$num = 7.89;
$temp = explode('.', $num);
echo str_pad($temp[0], 4, 0, STR_PAD_LEFT).'.'.str_pad($temp[1], 4, 0, STR_PAD_RIGHT);
Output
0007.8900
You can use sprintf("%f4.6"); The number before the dot would determine de number of digits fot the integer part and de other, the number of decimals