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 the number format function 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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Currency Format Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

  • For 3-formatter.php to work, you need to enable extension=intl in php.ini.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP CURRENCY FORMAT

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

 

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.

 

 

USEFUL BITS & LINKS

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 function – Do not use it anymore. It has been deprecated in PHP 7.4, and totally removed in PHP 8.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Format Currency In PHP (Click To Enlarge)

 

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 *