Removing currency symbol from formatCurrency

前端 未结 3 832
-上瘾入骨i
-上瘾入骨i 2021-01-19 06:26

I\'m trying to format revenue totals as grabbed from a db, and using php\'s NumberFormatter class, with the formatCurrency method.

However, I do not want to print ou

3条回答
  •  梦毁少年i
    2021-01-19 06:49

    I came here because for some reason, $formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, ''); was being ignored by $formatter->formatCurrency($array['total_revenue'], 'USD');

    To resolve this issue, I found out a solution here. https://www.php.net/manual/en/numberformatter.setsymbol.php#124153

    this could be obvious to some, but setSymbol(NumberFormatter::CURRENCY_SYMBOL, '') doesn't work for formatCurrency - it will simply be ignored...

    use NumberFormatter::CURRENCY and $fmt->format(123); to get a currency value with the symbol specified as CURRENCY_SYMBOL (or INTL_CURRENCY_SYMBOL)

    i.e

        $fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
        $fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
        $fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
        echo $fmt->format(56868993064.7985);
        //Output: 56.868.993.064,80 
    

提交回复
热议问题