PHP

Display a numeric string with 2 decimal places in PHP

0
3.7k
2018 / 05 / 07

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.

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks