NumberFormatter::parseCurrency
numfmt_parse_currency
(PHP 5 >= 5.3.0, PHP 7, PECL intl >= 1.0.0)
NumberFormatter::parseCurrency -- numfmt_parse_currency — Parse a currency number
Описание
Объектно-ориентированный стиль
$value
, string &$currency
[, int &$position
] )Процедурный стиль
$fmt
, string $value
, string &$currency
[, int &$position
] )Parse a string into a double and a currency using the current formatter.
Список параметров
-
fmt
-
NumberFormatter object.
-
currency
-
Parameter to receive the currency name (3-letter ISO 4217 currency code).
-
position
-
Offset in the string at which to begin parsing. On return, this value will hold the offset at which parsing ended.
Возвращаемые значения
The parsed numeric value or FALSE
on error.
Примеры
Пример #1 numfmt_parse_currency() example
<?php
$fmt = numfmt_create( 'de_DE', NumberFormatter::CURRENCY );
$num = "1.234.567,89\xc2\xa0$";
echo "We have ".numfmt_parse_currency($fmt, $num, $curr)." in $curr\n";
?>
Пример #2 OO example
<?php
$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
$num = "1.234.567,89\xc2\xa0$";
echo "We have ".$fmt->parseCurrency($num, $curr)." in $curr\n";
?>
Результат выполнения данного примера:
We have 1234567.89 in USD
Смотрите также
- numfmt_get_error_code() - Get formatter's last error code.
- numfmt_parse() - Parse a number
- numfmt_format_currency() - Format a currency value
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Internationalization Functions
- Функция NumberFormatter::create() - Create a number formatter
- Функция NumberFormatter::formatCurrency() - Format a currency value
- Функция NumberFormatter::format() - Format a number
- Функция NumberFormatter::getAttribute() - Get an attribute
- Функция NumberFormatter::getErrorCode() - Get formatter's last error code.
- Функция NumberFormatter::getErrorMessage() - Get formatter's last error message.
- Функция NumberFormatter::getLocale() - Get formatter locale
- Функция NumberFormatter::getPattern() - Get formatter pattern
- Функция NumberFormatter::getSymbol() - Get a symbol value
- Функция NumberFormatter::getTextAttribute() - Get a text attribute
- Функция NumberFormatter::parseCurrency() - Parse a currency number
- Функция NumberFormatter::parse() - Parse a number
- Функция NumberFormatter::setAttribute() - Set an attribute
- Функция NumberFormatter::setPattern() - Set formatter pattern
- Функция NumberFormatter::setSymbol() - Set a symbol value
- Функция NumberFormatter::setTextAttribute() - Set a text attribute
Коментарии
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 numberformatter.formatcurrency 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.