Display a numeric string with 2 decimal places in PHP

Display a numeric string with 2 decimal places in PHP

It can be done as:

$foo = "105";
echo number_format((float)$foo, 2, '.', '');

The output of above code is:  105.00

The same can be achieved as:

$foo = "105";
$number = sprintf('%0.2f', $foo);

and this:

$foo = "105.453465";
echo round($foo, 2);

Although, the purpose of round() function is different from other functions because round() is basically used for rounding off the values. See the details of round() function in this article.

Related Posts