Formatting money / currency using PHP
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.
Print money with decimals
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;
?>
Escaping special characters in PHP strings
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).
Print money without decimals
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;
?>
Further Reading
-
PHP number_format function
http://us2.php.net/manual/en/function.number-format.php
Article Comments
-
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 -
Thanks for your help..excellent snipplet!
-
What about none-standard currency?
Say I return 1347870 from a database query and wish to print it as: 134.78.70 ?
-
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? :)
-
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?
-
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
?>
-
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?
-
Hi Rob-
This should work:
<?php
// Will print $2.50
$number = 2.5;
$formatted = number_format($number,2);
?>
-
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 -
How about negative numbers?
How do I print something like -$250.00 ?please help.
-
You could just do:
// Ensure locale is properly set
setlocale(LC_MONETARY, ‘en_US’);
// Test amount
$number = 8675309.00
//Format currency according to money sign, and will handle negative values by default
$number_formatted = money_format(’%n’, $number);
print $number_formatted;
// prints $8,675,309.00Unfortunately, this won’t work on a Windows setup since it doesn’t have strfmon capabilities. Hey, just sayin’!
Post a Comment
You must be logged in to post a comment.