4 Ways To Format Number As Currency In PHP

Welcome to a quick tutorial and examples on how to format a number as a currency in PHP. Want to display a “nice amount” in your project?

The common ways to format a number as currency in PHP are:

  • Use number_format() to manually format the number to currency.
    • $amount = 1234.56;
    • $usd = "$" . number_format($amount, 2, ".", ",");
    • $jpy = "¥" . number_format($amount, 0, ".", ",");
  • Use regular expressions.
    • $amount = 6543.21;
    • $regex = "/\B(?=(\d{3})+(?!\d))/i";
    • $usd = "$" . preg_replace($regex, ",", $amount);

That covers the quick basics, but read on for more methods and examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Click here to download

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP CURRENCY FORMAT

All right, let us now get into the various ways and examples to format a currency in PHP.

 

TUTORIAL VIDEO

 

METHOD 1) NUMBER FORMAT

1-number-format.php
<?php
// (A) THE NUMBER
$amount = 6543.21;
 
// (B) TO USD - $6,543.21
$usd = "$" . number_format($amount, 2, ".", ",");
echo $usd;
 
// (C) TO JPY - ¥6,543
$jpy = "¥" . number_format($amount, 0, ".", ",");
echo $jpy;
 
// (D) EURO (GERMANY) - 6.543,21€
$euro = number_format($amount, 2, ",", ".") . "€";
echo $euro;
 
// (E) EURO (FRENCH) - 6 543,21€
$euro = number_format($amount, 2, ",", " ") . "€";
echo $euro;

Yep, the number format function is a very common solution. Here are 4 parameters – number_format(AMOUNT, DECIMAL PLACES, THOUSANDS SEPARATOR, DECIMAL SEPARATOR). Don’t think it needs any further explanation.

 

METHOD 2) REGULAR EXPRESSION

2-regular-expression.php
<?php
// (A) THE NUMBER
$amount = 6543.21;
 
// (B) REGULAR EXPRESSION
$regex = "/\B(?=(\d{3})+(?!\d))/i";
$usd = "$" . preg_replace($regex, ",", $amount);
echo $usd;

The dreaded regular expressions… I am not going to explain this in detail, as it is very confusing and a whole can of worms on its own. But in plain English, all this does is “insert a comma for every 3 digits (thousands)”.

 

 

METHOD 3) NUMBER FORMATTER

3-formatter.php
<?php
// (A) THE NUMBER
$amount = 6543.21;
 
// (B) TO USD - $6,543.21
$nf = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
$usd = $nf->formatCurrency($amount, "USD");
echo $usd;
 
// (C) TO JPY - ¥6,543
$nf = new NumberFormatter("ja_JP", NumberFormatter::CURRENCY);
$jpy = $nf->formatCurrency($amount, "JPY");
echo $jpy;
 
// (D) TO EURO (FRANCE) - 6 543,21 €
$nf = new NumberFormatter("fr_FR", NumberFormatter::CURRENCY);
$eur = $nf->formatCurrency($amount, "EUR");
echo $eur;

This is probably the next easiest way to format a currency in PHP, but make sure that extension=intl is enabled in php.ini.

 

METHOD 4) MANUAL FORMAT

4-manual.php
<?php
// (A) FORMAT CURRENCY (DOLLARS)
function curformat ($amount) {
  // (A1) SPLIT WHOLE & DECIMALS
  $amount = explode(".", $amount);
  $whole = $amount[0];
  $decimal = isset($amount[1]) ? $amount[1] : "00" ;
 
  // (A2) ADD THOUSAND SEPARATORS
  if (strlen($whole) > 3) {
    $temp = ""; $j = 0;
    for ($i=strlen($whole)-1; $i>=0; $i--) {
      $temp = $whole[$i] . $temp;
      $j++;
      if ($j%3==0 && $i!=0) { $temp = "," . $temp; }
    }
    $whole = $temp;
  }
 
  // (A3) RESULT
  return "\$$whole.$decimal";
}
 
// (B) TEST!
echo curformat(54321); // $54,321.00
echo curformat(12345.67); // $12,345.67

Finally, the “brute force” way to manually format a currency… At least you get to control whatever format you want.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

MONEY FORMAT?

In some of the older online tutorials, you will see some people use a money_format() – money format function. Do not use it anymore. It has been deprecated in PHP 7.4, and totally removed in PHP 8.

 

INFOGRAPHIC CHEATSHEET

Format Number As Currency In PHP (Click To Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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