Page 1 of 1

Re: Maintaining number of digits after decimal points.

Posted: Tue Nov 02, 2010 5:09 am
by klevis miho
Try number_format:

$a = '2';
$b = '2.6';
$c = '3.55';

echo number_format($a, 2, '.', '') . '<br>';
echo number_format($b, 2, '.', '') . '<br>';
echo number_format($c, 2, '.', '') . '<br>';

Re: Maintaining number of digits after decimal points.

Posted: Tue Nov 02, 2010 3:26 pm
by abhay440
number_format will only format the digits after decimal places. Use padding to prefix your number with '0'. I prefer to use printf and sprintf for formatting.

Code: Select all

$a = '2.6';
$temp = sprintf("%.2f", $a);
$result = sprintf("%05s", $temp);
echo $result;  //Outputs: 02.60

Re: Maintaining number of digits after decimal points.

Posted: Tue Nov 09, 2010 11:09 am
by requinix
You can actually combine those two into %05.2f.

sprintf manual page