As a web developer, there will come a time when you’ll need to work with money. Whether it’s for a fully operational shopping cart or a simple calculator, PHP makes this an incredibly simple process.
For the purposes of this tutorial, let’s assume we have numeric variable with a value of 21357.44. This value could have been pulled from a database, or calculated via some other means.
To format the number in US currency, use PHP’s built-in “number_format” function like so:
<?php
// Our original decimal number
$number = 21357.44;
// Let’s use PHP’s built-in function to format the number into US currency
$formatted = number_format($number,2);
// The following statement will print 21,357.44
echo $formatted;
?>
As you can see, not too hard at all. I passed the number_format function two parameters: “$number,” which is the variable I want to format, and “2,” which is the amount of decimal places I want to display. We did forget one thing, though– the dollar sign ($) in front of our number. This is easily solved by:
<?php
// Our original decimal number
$number = 21357.44;
// Let’s use PHP’s built-in function to format the number into US currency
$formatted = number_format($number, 2);
// The following statement will print 21,357.44
echo "\$" . $formatted;
?>
Notice the “\” in front of the dollar sign. Certain characters, like “$,” have special meanings in PHP. To avoid any unexpected errors, we add the “\” to escape the special character. This tells PHP to treat the character as plain text (a string), and avoid any special processing it would have otherwise done (in this case, the $ is special because it’s used to denote variables in PHP).
What if we want to remove the values after the decimal point and just print the whole number? There are plenty of ways to do this, including string manipulation, but the number_format function can take care of this on its own by slightly modifying our original function call.
<?php
// Our original decimal number
$number = 21357.44;
// Let’s use PHP’s built-in function to format the number into US currency
$formatted = number_format($number);
// The following statement will print 21,357
echo "\$" . $formatted;
?>
You must be logged in to post a comment.





November 1st, 2005 at 11:05 am
Very quick and to the point. Well written and has helped me get this little issue solved.
Chad R. Smith
Director of Web Development
http://www.50marketing.com
March 10th, 2006 at 12:27 am
Thanks for your help..excellent snipplet!
April 14th, 2006 at 1:54 am
What about none-standard currency?
Say I return 1347870 from a database query and wish to print it as: 134.78.70 ?
April 24th, 2006 at 1:20 am
echo “\$” . $formatted;
Since you’re using the dot concatenation operator, you might as well eliminate the need to scan through the double-quoted string, like so –
echo ‘$’ . $formatted;
If you use single quotes, PHP won’t attempt to parse the string. It will simply take it as it is. This is only a tiny bit more efficient of course, but little things do tend to add up. Besides, why be inefficient when you can be efficient? :)
November 19th, 2006 at 6:23 pm
How about this …
I query a DB where currency is stored as a varchar(10) and I want to add up the total in my php script. What do I do then?
November 19th, 2006 at 11:15 pm
Plankton-
Try the code below. I commented areas to indicate what each code block is doing.
<?php
/*
This assumes that the column(field) you are selecting is called column, and the table is called your_table.
*/
$total = 0; // Declare total variable
$sql="SELECT column FROM your_table"; // SQL statement to run
$result=mysql_query($sql); // Query database
// Loops through query result and adds each value to $total
while ($row = mysql_fetch_array($result)) {
$total = $total + $row[column];
}
echo $total; // Prints total to screen
?>
December 13th, 2006 at 12:36 pm
Works great minus one problem, if I have a dollar amount such as $2.50 it does not echo the 0, thus showing up as $2.5
What would I do to force the currency to show the amount as $2.50?
December 13th, 2006 at 8:24 pm
Hi Rob-
This should work:
<?php
// Will print $2.50
$number = 2.5;
$formatted = number_format($number,2);
?>
November 16th, 2007 at 3:45 am
how about this :
if I want to view with format “###,###,###.###”
and i have value 100,000,000.010
and will display 100,000,000.01