Removing currency symbol from formatCurrency

前端 未结 3 834
-上瘾入骨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条回答
  • 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 
    
    0 讨论(0)
  • 2021-01-19 06:52

    A simple regex is a quick fix for your problem. Try;

    $actual = $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
    $output = preg_replace( '/[^0-9,"."]/', '', $actual );
    echo $output;
    

    Hope this helps

    0 讨论(0)
  • 2021-01-19 06:52

    You should use setSymbol() function:

    $formatter = new NumberFormatter('en_GB',  NumberFormatter::CURRENCY);
    $formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
    echo $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
    
    0 讨论(0)
提交回复
热议问题