PHP 7.2.0 Release Candidate 2 Released

NumberFormatter::parseCurrency

numfmt_parse_currency

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

NumberFormatter::parseCurrency -- numfmt_parse_currencyAnalyse un nombre monétaire

Description

Style orienté objet

public float NumberFormatter::parseCurrency ( string $value , string &$currency [, int &$position ] )

Style procédural

float numfmt_parse_currency ( NumberFormatter $fmt , string $value , string &$currency [, int &$position ] )

Analyse une chaîne dans un nombre décimal, et une devise, à l'aide du formateur.

Liste de paramètres

fmt

L'objet NumberFormatter.

currency

Le nom de la devise (le code 3 lettres ISO 4217).

position

La position de début d'analyse dans la chaîne. En retour, cette valeur contiendra la position de fin d'analyse.

Valeurs de retour

Le nombre décimal ainsi lu, ou FALSE en cas d'erreur.

Exemples

Exemple #1 Exemple avec numfmt_parse_currency(), Style procédural

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

Exemple #2 Exemple avec numfmt_parse_currency(), Style procédural

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

L'exemple ci-dessus va afficher :

We have 1234567.89 in USD

Voir aussi

add a note add a note

User Contributed Notes 2 notes

up
2
daniel at danielphenry dot com
2 years ago
The given examples confused me a bit. This may be a bit more clear:

$region = 'en_US';
$currency = 'USD';
$formatter = new NumberFormatter($region, NumberFormatter::CURRENCY);
echo $formatter->parseCurrency(12543.67, $currency);

Responds with:

$12,543.67
up
0
info at mm-newmedia dot de
2 months 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.
To Top