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>';
Maintaining number of digits after decimal points.
Moderator: General Moderators
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: Maintaining number of digits after decimal points.
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