Sometimes, we want to show a number to two decimal places with PHP.
In this article, we’ll look at how to show a number to two decimal places with PHP.
How to show a number to two decimal places with PHP?
To show a number to two decimal places with PHP, we can use the number_format
function.
For instance, we write
$foo = "105";
echo number_format((float)$foo, 2, '.', '');
to call number_format
with $foo
converted to a float , 2 and '.'
to return a string with 105 rounded to 2 decimal places.
Conclusion
To show a number to two decimal places with PHP, we can use the number_format
function.