Adding 0to the number, the rightmost zeros are removed:
$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;
$num1 = number_format(99.00, 2, '.', '') + 0;
$num2 = number_format(99.90, 2, '.', '') + 0;
$num3 = number_format(99.01, 2, '.', '') + 0;
$num4 = number_format(99.012, 2, '.', '') + 0;
$num5 = number_format(99.0123, 2, '.', '') + 0;
echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";
Output:
99
99.9
99.01
99.01
99.01
Try it here .
With the help offunction :
function round2no0(&$num)
{
$num = number_format($num, 2, '.', '') + 0;
}
using:
$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;
round2no0($num1);
round2no0($num2);
round2no0($num3);
round2no0($num4);
round2no0($num5);
echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";
function round2no0(&$num)
{
$num = number_format($num, 2, '.', '') + 0;
}
Output:
99
99.9
99.01
99.01
99.01
Edit:
Added parameters , '.', ''in number_formatto handle also numbers with thousands supporting the machine format 12345.12.
Try it here .