简体   繁体   中英

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

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.

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" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM