You can format any numeric value to have decimal points by using PHP’s
number_format() function. This is ideal when you want to deal with currency values. In the below example, I have shown how to do it.
<?php $price = 10; echo $price; // outputs 10 // here we've used number_format() function to add decimals to the $price $price_with_decimals = number_format($price, 2, '.', ''); echo $price_with_decimals; // outputs 10.00
On line 2 we have defined our
$price variable with its value set to 10. On line 3 we have printed it. You will see there’s no decimal part in the output. Then on line 6, we have used the
number_format() function to add 2 decimal points to the
$price and assign it resulting value to a new variable named
$price_with_decimals. Later when we print it out (on line 9), you’ll see the price is now showing with two decimal points added.
You can learn more about the usage of
number_format() function by visiting its official PHP documentation.
See you soon! 👋👋👋