phpbrowsercharacter-encodingnumberformatter

Why do some web browsers incorrectly display currencies produced by NumberFormatter


Why some older web browsers (not all) incorrectly display currency symbols when symbols are produced by php NumberFormatter() but display correct symbol when given as html entity. Since correct symbol is displayed via HTML entity code it means that browser has the font to display the currency symbol correctly. For example SRWare Iron v.49 behaves like this.

NOTE: I used an asterisk at front of the price below (*90,023) but the browsers in question actually display a little square in place of currency symbol.

$locale = 'ja-JP';
$currency = 'JPY';                                  
$formatter = new NumberFormatter($locale."@currency=$currency", NumberFormatter::CURRENCY);
header("Content-Type: text/html; charset=UTF-8;");     
print htmlspecialchars($formatter->formatCurrency('90023.12', $currency));

Output: *90,023  //Incorrect output, should be  ¥90,023

print '¥';  //HTML entity for Japanese Yen

Output: ¥        //Correct currency symbol when given as html entity

Solution

  • There are actually two Yen characters defined in Unicode:

    NumberFormatter uses the second one, as shown by this test:

    $locale = 'ja-JP';
    $currency = 'JPY';                                  
    $formatter = new NumberFormatter($locale."@currency=$currency", NumberFormatter::CURRENCY);
    $s1 = $formatter->formatCurrency(1, $currency);
    $s2 = "\u{FFE5}1";
    var_dump($s1 == $s2);
    

    Outputs true (demo).

    If no font with that character is available, then it will not show correctly (unless the browser is smart enough to substitute it with the usual Yen character).