In this tutorial, we will learn how to format numbers to 2 decimals in PHP.
PHP format number with 2 decimals
To format a number with two decimals in PHP, you can use the number_format
function.
Here's an example:
$number = 1234.5678;
// Format the number with 2 decimals
$formattedNumber = number_format($number, 2);
echo $formattedNumber;
In the above example, $number
is the original number (1234.5678), and number_format($number, 2)
formats it to have two decimals.
Output
1,234.57
You can replace $number
with your own numeric value, and the number_format
function will format it accordingly.