Simple web design, web development, information technology, & technical communication

Articles and resources for web developers and the average person. Topics include web development, web design, technical writing and communication, and all aspects of information technology.

Formatting money / currency using PHP

Originally published on Sunday, September 18th, 2005

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

9 Responses to “Formatting money / currency using PHP”

  1. Chad R. Smith Says:

    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

  2. Bill Kelley Says:

    Thanks for your help..excellent snipplet!

  3. Foxy Says:

    What about none-standard currency?

    Say I return 1347870 from a database query and wish to print it as: 134.78.70 ?

  4. Denny Says:

    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? :)

  5. plankton Says:

    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?

  6. Christopher Jason Says:

    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
    ?>

  7. Rob Bursmith Says:

    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?

  8. Christopher Jason Says:

    Hi Rob-

    This should work:

    <?php
    // Will print $2.50
    $number = 2.5;
    $formatted = number_format($number,2);
    ?>

  9. parwanto Says:

    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

Add a Comment

You must be logged in to post a comment.

Articles By Category

Stay Updated