PHP 7.0.6 Released

NumberFormatter::formatCurrency

numfmt_format_currency

(PHP 5 >= 5.3.0, PHP 7, PECL intl >= 1.0.0)

NumberFormatter::formatCurrency -- numfmt_format_currencyFormat a currency value

Description

Object oriented style

public string NumberFormatter::formatCurrency ( float $value , string $currency )

Procedural style

string numfmt_format_currency ( NumberFormatter $fmt , float $value , string $currency )

Format the currency value according to the formatter rules.

Parameters

fmt

NumberFormatter object.

value

The numeric currency value.

currency

The 3-letter ISO 4217 currency code indicating the currency to use.

Return Values

String representing the formatted currency value.

Examples

Example #1 numfmt_format_currency() example

<?php
$fmt 
numfmt_create'de_DE'NumberFormatter::CURRENCY );
echo 
numfmt_format_currency($fmt1234567.891234567890000"EUR")."\n";
echo 
numfmt_format_currency($fmt1234567.891234567890000"RUR")."\n";
$fmt numfmt_create'ru_RU'NumberFormatter::CURRENCY );
echo 
numfmt_format_currency($fmt1234567.891234567890000"EUR")."\n";
echo 
numfmt_format_currency($fmt1234567.891234567890000"RUR")."\n";
?>

Example #2 OO example

<?php
$fmt 
= new NumberFormatter'de_DE'NumberFormatter::CURRENCY );
echo 
$fmt->formatCurrency(1234567.891234567890000"EUR")."\n";
echo 
$fmt->formatCurrency(1234567.891234567890000"RUR")."\n";
$fmt = new NumberFormatter'ru_RU'NumberFormatter::CURRENCY );
echo 
$fmt->formatCurrency(1234567.891234567890000"EUR")."\n";
echo 
$fmt->formatCurrency(1234567.891234567890000"RUR")."\n";
?>

The above example will output:

1.234.567,89 €
1.234.567,89 RUR
1 234 567,89€
1 234 567,89р.

See Also

User Contributed Notes

Ruben
3 years ago
While this function accepts floats for currency (in order to display cents), you should (for applications where this is critical) never store or handle money using floats, as rounding errors may occur. Work with integers (or a BigInt class if integers aren't large enough) internally instead, where the integer represents the total number of cents. An alternative (especially if you need more precision than cents) is using the BC (Binary Calculator) Math module, that handles arbitrary precision numbers with 100% accuracy.
martin t holzhauer dohd eu
2 years ago
When you want to format currency's without sub units and the currency is not the one used by the given locale you need to set the currency code before as TextAttribute _BEFORE_ setting the NumberFormatter::FRACTION_DIGITS

<?php
$fmt
= new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$fmt->setTextAttribute(NumberFormatter::CURRENCY_CODE, 'EUR');
$fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
$fmt->formatCurrency(100, 'EUR');
?>
Tyler Crompton
1 year ago
This had me scratching my head. When working with certain English locales (e.g. "en_US" and "en_CA" among others but certainly not all), it is important to note that negative numbers are formatted differently between PHP 5.5 and PHP 5.6.

Code:

<?php

$formatter
= new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo
$formatter->formatCurrency(-0.99, 'USD'), PHP_EOL;

$formatter = new NumberFormatter('en_CA', NumberFormatter::CURRENCY);
echo
$formatter->formatCurrency(-0.99, 'USD'), PHP_EOL;

?>

Output from PHP 5.5:

-$0.99
-US$0.99

Output from PHP 5.6:

($0.99)
(US$0.99)
Benoit Borrel
5 months ago
When setting the pattern, don't forget that space character between currency symbol and number (either as prefix or suffix) should not be breakable (like &nbsp; for HTML). For example, in UTF-8 you should use the no-break-space character ("\xC2\xA0"):
<?php
$fmt
= new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$fmt->setPattern(str_replace('¤#',"¤\xC2\xA0#", $fmt->getPattern()));
?>
mail at cebe dot cc
1 year ago
The note about different formatting[1] actually does not depend on the PHP version but on the version of the icu library[2] that PHP is compiled against because this library has a database with formatting rules for the different locales.

[1]: http://php.net/manual/en/numberformatter.formatcurrency.php#116610
[2]: http://site.icu-project.org/
idragosalex
11 months ago
It seams that for currency symbols that contain the dollar sign '$' the resulting formatted number is broken: 95.23 is formatted .23 instead of $95.23. As a workaround, you need to modify the pattern by adding a space:

<?php
$fmt
= new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$fmt->setTextAttribute(NumberFormatter::CURRENCY_CODE, 'CAD');
$fmt->setPattern( str_replace('¤#','¤ #', $fmt->getPattern() ) );
echo
$fmt->formatCurrency(100, 'CAD');
?>
To Top