NumberFormatter::formatCurrency

numfmt_format_currency

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

NumberFormatter::formatCurrency -- numfmt_format_currency β€” Π€ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π²Π°Π»ΡŽΡ‚Ρ‹

ОписаниС

ΠžΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ

public function NumberFormatter::formatCurrency(float $amount, string $currency): string|false

ΠŸΡ€ΠΎΡ†Π΅Π΄ΡƒΡ€Π½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ

function numfmt_format_currency(NumberFormatter $formatter, float $amount, string $currency): string|false

Π€ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π²Π°Π»ΡŽΡ‚Ρ‹ Π² соотвСтствии с ΠΏΡ€Π°Π²ΠΈΠ»Π°ΠΌΠΈ форматирования.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

formatter

ΠžΠ±ΡŠΠ΅ΠΊΡ‚ NumberFormatter.

amount

ЧисловоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π²Π°Π»ΡŽΡ‚Ρ‹.

currency

Π’Ρ€Ρ‘Ρ…Π±ΡƒΠΊΠ²Π΅Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π²Π°Π»ΡŽΡ‚Ρ‹ ISO 4217, ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°ΡŽΡ‰ΠΈΠΉ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΡƒΡŽ Π²Π°Π»ΡŽΡ‚Ρƒ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π‘Ρ‚Ρ€ΠΎΠΊΠ°, ΠΏΡ€Π΅Π΄ΡΡ‚Π°Π²Π»ΡΡŽΡ‰Π°Ρ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π²Π°Π»ΡŽΡ‚Ρ‹ ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования numfmt_format_currency()

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

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Π² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠΌ стилС

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

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

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

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π€ΠΎΡ€ΠΌΠ°Ρ‚Ρ‹, достиТимыС этим способом форматирования, Π½Π΅ ΠΌΠΎΠ³ΡƒΡ‚ ΠΏΠΎΠ»Π½ΠΎΡΡ‚ΡŒΡŽ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ возмоТности Π±Π°Π·ΠΎΠ²ΠΎΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ ICU, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π²Π°Π»ΡŽΡ‚Ρƒ с ΡƒΠ·ΠΊΠΈΠΌ символом Π²Π°Π»ΡŽΡ‚Ρ‹.

Для ΠΏΠΎΠ»Π½ΠΎΠΉ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ msgfmt_format_message().

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • numfmt_get_error_code() - ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ послСдний ΠΊΠΎΠ΄ ошибки срСдства форматирования
  • numfmt_format() - Π€ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ число
  • numfmt_parse_currency() - Π Π°Π·Π±ΠΈΡ€Π°Π΅Ρ‚ Π½ΠΎΠΌΠ΅Ρ€ Π²Π°Π»ΡŽΡ‚Ρ‹
  • msgfmt_format_message() - Быстро Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ сообщСниС
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 8 notes

up
50
Ruben ΒΆ
13 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.
up
28
martin t holzhauer dohd eu ΒΆ
12 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');
?>
up
9
mail at cebe dot cc ΒΆ
11 years 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/
up
4
Tyler Crompton ΒΆ
11 years 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)
up
2
Benoit Borrel ΒΆ
10 years 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()));
?>
up
2
Anonymous ΒΆ
8 years ago
formatCurrency() does not follow international standard for currency decimal, published here : https://www.currency-iso.org/en/home/tables/table-a1.html.

To define decimal I found that we need to use format() function after setting some NumberFormat attributes.

For example "COP" (Colombian peso) if defined to use 2 decimals, but NumberFormat::formatCurrency() use 0 decimal for this currency (I do not know why!).

Here is the code I use :

$fmt = new \NumberFormatter( 'fr', \NumberFormatter::CURRENCY);
$fmt->setTextAttribute( $fmt::CURRENCY_CODE, 'COP' );
$fmt->setAttribute( $fmt::FRACTION_DIGITS, 2 );
$numberString = $fmt->format( 1234.56 );

The output is: 1 234,56Β $CO

If locale change to 'en' then the output is : COP1,234.56
up
0
andrewfenn at gmail dot com ΒΆ
9 years ago
Currency symbols seem to have spurious support.

Take for example the Thai Baht symbol.. ΰΈΏ which doesn't seem supported in the Thai locale, but is in other locales such as Chinese Simplified..

<?php
$fmt = new NumberFormatter('th_TH', NumberFormatter::CURRENCY);
echo $fmt->formatCurrency(100, 'THB');
// Outputs: THB 100

$fmt = new NumberFormatter('zh_Hans', NumberFormatter::CURRENCY);
echo $fmt->formatCurrency(100, 'THB');
// Outputs: ΰΈΏ 100
?>
up
-1
Patanjali ΒΆ
6 years ago
This function is typically over a 1000 times slower on the first run in a php session compared to subsequent runs, and that is using a newly created formatter each time.

Timings for the first run have been from 60ms to 195ms, whereas subsequent runs are well under 100us.

For comparison, creating the formatter takes about 100us.