4

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.

  • Is this bug reported same as yours https://bugs.php.net/bug.php?id=75371 ? – nice_dev Jan 22 '20 at 11:11
  • Asked the resident Swede in our office - she agrees with PHP for currency formatting in Sweden (i.e. spaces for thousand separators)... as do Ikea. Your format looks like that used in mainland Europe (NL/DE/FR...) rather than Sweden... – CD001 Jan 22 '20 at 11:21
  • @vivek_23 Well, I didn't file it, but it seems related. –  Jan 22 '20 at 11:44
  • @CD001 I should have mentioned that in Sweden, non-monetary numbers are formatted as PHP formatted it, but money sums use periods for thousands separators. –  Jan 22 '20 at 11:44
  • 1
    just do `str_replace()` of whitespaces with dots within the produced number. – mitkosoft Jan 22 '20 at 11:46
  • @user12760574 - that's curious, PHP has *always* (according to 3v4l.org testing different versions) formatted numbers and currencies the same for Sweden - and it seems the use of space for the thousands separator (rightly or wrongly) is pretty common for prices on SE websites. You *could* use [NumberFormatter::setPattern()](https://www.php.net/manual/en/numberformatter.setpattern.php) to enforce the behavior you want? I know it's not ideal, but it would work. – CD001 Jan 22 '20 at 12:04
  • @mitkosoft Well, yeah, I could easily do such a "quick fix", but it doesn't tell me if it also has other mistakes. –  Jan 22 '20 at 12:20
  • @CD001 Sadly, "all is lost" for websites/stores and using correct number/money sum formatting. They just do whatever they think looks best, with no regard to the actual locale rules. :( –  Jan 22 '20 at 12:20

1 Answers1

0

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" 
Adder
  • 5,708
  • 1
  • 28
  • 56
  • Thanks, and I will likely use it, but it still gives me a nagging feeling that if this was wrong, what else is also wrong which I will never know because I don't have special knowledge about any other language/locale/currency combo? That's what really worries me! –  Jan 22 '20 at 12:21
  • @user12760574 According to https://sv.wikipedia.org/wiki/Tusentalsavgr%C3%A4nsare the space is recommended in Sweden, also for currencies. So, based on this there is no need to worry... – JanP Jan 22 '20 at 12:39
  • @JanP Hmm. Well, what does it mean that it's "recommended"? I like to do things correctly rather than what's popular. –  Jan 22 '20 at 21:23