Have I found a bug in PHP's NumberFormatter's "formatCurrency" function?

573 Views Asked by At

I happen to know for a fact how the "SEK" (Swedish money) currency is supposed to be formatted in "SE" (Sweden) locale with "sv" (Swedish) language. It's supposed to be using periods for thousands separators. However, the following minimal code example will show that PHP outputs it with spaces instead of periods for the thousands separators. It does get the comma right (for decimal character) as well as the " kr" in the end, so I don't know what to make of this:

// Code example #1 (working correctly):

$a = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
var_dump($a->formatCurrency(123456789.987, 'USD'));

Expected output:

    $123,456,789.99

Actual output:

    $123,456,789.99

// Code example #2 (seemingly not working correctly):

$a = new \NumberFormatter('sv_SE', \NumberFormatter::CURRENCY);
var_dump($a->formatCurrency(123456789.987, 'SEK'));

Expected output:

    123.456.789,99 kr

Actual output:

    123 456 789,99 kr

What possible reason could there be for this? It's so frustrating that it "almost" got it right.

1

There are 1 best solutions below

3
On

This works, but it is a hack:

$a = new \NumberFormatter('sv_SE', \NumberFormatter::CURRENCY);
$a->setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '.'); 
$a->setPattern('#,##0.## kr');
var_dump($a->formatCurrency(123456789.987, 'SEK'));

Output:

string(17) "123.456.789,99 kr"