Formatting Currency Values

You can format any numeric value to have decimal points by the use of PHP’s number_format() function. This is ideal when you want to deal with currency values. Below I have shown an example of how this works:

<?php

$price = 10;

echo $price;     // outputs 10

echo number_format(number_format($price, 2, '.', ''));      // outputs 10.00

On line 3 we have the $price variable with its value set to 10. On line 5 we have printed it. You will see there’s no decimal part in the output. Then on line 7, we have used the number_format() function to add 2 decimal points to the $price. So you will see its outputting 10.00.

Here is the link to number_format function documentation

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar



Do NOT follow this link or you will be banned from the site!