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!
ⓘ 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.
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
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 enableextension=intl
inphp.ini
.
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
<?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
<?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
<?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
.
- Using the number formatter is a 2-steps process.
- Create
$nf = new NumberFormatter(LANGUAGE_REGION);
- Use it to format the currency –
$amt = $nf->formatCurrency(AMOUNT, CURRENCY);
- Create
- For the uninitiated, that
LANGUAGE_REGION
is actually a pair of ISO codes. For example,en_US
is “US English”,en_UK
is “UK English”, andja_JP
is… Japan Japanese.
METHOD 4) MANUAL FORMAT
<?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.
EXTRA 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()
– money format function. Do not use it anymore. It has been deprecated in PHP 7.4, and totally removed in PHP 8.
LINKS & REFERENCES
- Number Format – PHP
- Regular Expressions – Tutorials Point
- Format Currency – PHP
INFOGRAPHIC CHEAT SHEET

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!