phpintleuronumberformatter

NumberFormatter parse Euro's with comma as decimal splitter


I've a big headache over this. Why isn't this code working?

<?php
setlocale(LC_MONETARY, 'nl');
$fmt = new NumberFormatter( 'nl', NumberFormatter::CURRENCY );
$num = "€2,50";
echo "We have ".$fmt->parseCurrency($num, $curr)." in $curr\n";
?>

I've tried this on both a Windows machine and at http://phpfiddle.org/. The expected outcome is We have 2.50 in EUR.

The dutch locale is as following:

>>> localeconv()
=> [
     "decimal_point" => ",",
     "thousands_sep" => ".",
     "int_curr_symbol" => "EUR",
     "currency_symbol" => b"€",
     "mon_decimal_point" => ",",
     "mon_thousands_sep" => ".",
     "positive_sign" => "",
     "negative_sign" => "-",
     "int_frac_digits" => 2,
     "frac_digits" => 2,
     "p_cs_precedes" => 1,
     "p_sep_by_space" => 1,
     "n_cs_precedes" => 1,
     "n_sep_by_space" => 1,
     "p_sign_posn" => 4,
     "n_sign_posn" => 4,
     "grouping" => [
       3,
     ],
     "mon_grouping" => [
       3,
     ],
   ]

Solution

  • The currency parser is super strange and expects a non breaking space UTF-8 sign between the currency symbol and the number. This example works fine for me:

    <?php
    setlocale(LC_MONETARY, 'nl');
    $fmt = new NumberFormatter( 'nl', NumberFormatter::CURRENCY );
    $num = "€\xc2\xa02,50";
    echo "We have ".$fmt->parseCurrency($num, $curr)." in $curr\n";
    ?>
    

    \xc2\xa0 is the code for that breaking space.