Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing currency symbol from formatCurrency

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 out the actual € / Euro symbol with this. I just want the plain number, with comma's and decimal points.

Example; 1234.56 should be formatted as 1,234.56 The current output is giving €1,234.56.

Code I'm using:

$array['total_revenue'] = $this
    ->db
    ->query($sql)
    ->row_array()['SUM( booking_total )'];

$formatter = new NumberFormatter('en_GB',  NumberFormatter::CURRENCY);

echo $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;

Would anyone have any ideas on how I can fix this up to remove the euro symbol?

like image 340
Eoghan Avatar asked Nov 26 '15 17:11

Eoghan


People also ask

How do I remove currency symbol from Number in Excel?

Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want. Note: If you want to display a monetary value without a currency symbol, you can click None.

How do I remove the currency symbol in access?

To remove the '$' symbol, you can change the format from Currency to General or you can manually type 0# in the Format tab.

How do I make the currency sign on my keyboard?

123 key in the lower-left corner of the keyboard. In the second row of numbers and symbols, press and hold your finger on the dollar-sign key. Above your finger, a box will pop up showing several currency symbols; these include the peso, the euro, the cent sign, the pound sterling and the yen.


2 Answers

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;
like image 183
Marek Skiba Avatar answered Nov 07 '22 07:11

Marek Skiba


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 
like image 37
steven7mwesigwa Avatar answered Nov 07 '22 07:11

steven7mwesigwa