NumberFormatter::parseCurrency

numfmt_parse_currency

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

NumberFormatter::parseCurrency -- numfmt_parse_currency β€” Π Π°Π·Π±ΠΈΡ€Π°Π΅Ρ‚ Π½ΠΎΠΌΠ΅Ρ€ Π²Π°Π»ΡŽΡ‚Ρ‹

ОписаниС

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

public function NumberFormatter::parseCurrency(string $string, string &$currency, int &$offset = null): float|false

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

function numfmt_parse_currency(
Β Β Β Β NumberFormatter $formatter,
Β Β Β Β string $string,
Β Β Β Β string &$currency,
Β Β Β Β int &$offset = null
): float|false

Π Π°Π·Π±ΠΈΡ€Π°Π΅Ρ‚ строку Π½Π° число с ΠΏΠ»Π°Π²Π°ΡŽΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ ΠΈ Π²Π°Π»ΡŽΡ‚Ρƒ с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ срСдства форматирования.

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

formatter

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

currency

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ для получСния названия Π²Π°Π»ΡŽΡ‚Ρ‹ (Ρ‚Ρ€Ρ‘Ρ…Π±ΡƒΠΊΠ²Π΅Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π²Π°Π»ΡŽΡ‚Ρ‹ ISO 4217).

offset

Π‘ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ Π² строкС, с ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ начинаСтся синтаксичСский Π°Π½Π°Π»ΠΈΠ·. ΠŸΡ€ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π΅ это Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ смСщСниС, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ закончился синтаксичСский Π°Π½Π°Π»ΠΈΠ·.

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

Π Π°Π·ΠΎΠ±Ρ€Π°Π½Π½ΠΎΠ΅ числовоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΈΠ»ΠΈ false Π² случаС возникновСния ошибки.

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

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

<?php
$fmt
= numfmt_create( 'de_DE', NumberFormatter::CURRENCY );
$num = "1.234.567,89\xc2\xa0$";
echo
"У нас ".numfmt_parse_currency($fmt, $num, $curr)." в $curr\n";
?>

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

<?php
$fmt
= new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
$num = "1.234.567,89\xc2\xa0$";
echo
"У нас ".$fmt->parseCurrency($num, $curr)." в $curr\n";
?>

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

У нас 1234567.89 в USD

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

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

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

up
3
info at mm-newmedia dot de ΒΆ
8 years ago
In reply to daniel at danielphenry dot com example note beneath. The given example by Daniel returns false under PHP7.x, which is a normal behavior since NumberFormatter::parseCurrency() is a method for parsing currency strings. It is trying to split up the given string in a float and a currency. 

While using strict types under PHP7 the following example makes it more clearer.

<?php
declare(strict_types=1);
namespace MMNewmedia;

$oParser = new \NumberFormatter('de_DE', \NumberFormatter::CURRENCY);
var_dump($oParser->parseCurrency("1.234.567,89\xc2\xa0€", $currency), $currency));
?>

This example returns: "float(1234567.89) string(3) "EUR"

This is the expected behavior.

The following example runs into a type error, which is absolutely right, since this method is vor parsing strings and not vor formatting floats into currency strings.

<?php
declare(strict_types=1);
namespace MMNewmedia;

try {
    $oCurrencyParser = new \NumberFormatter('de_DE', \NumberFormatter::CURRENCY);
    $currency = 'EUR';
    var_dump($oCurrencyParser->parseCurrency(1.234, $currency), $currency);
} catch (\TypeError $oTypeError) {
    var_dump($oTypeError->getMessage());
}
?>

This example returns "NumberFormatter::parseCurrency() expects parameter 1 to be string, float given".

If you want to parse floats into a currency string use the http://php.net/manual/en/numberformatter.formatcurrency.php method as shown in the next example.

<?php
declare(strict_types=1);
namespace MMNewmedia;

$oFormatter = new \NumberFormatter('de_DE', \NumberFormatter::CURRENCY);
var_dump($oFormatter->formatCurrency(1234567.89, 'EUR'));
?>

This returns string(17) "1.234.567,89 €" as expected.