Reputation: 1761
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?
Upvotes: 9
Views: 11014
Reputation: 6710
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 forformatCurrency
- it will simply be ignored...use
NumberFormatter::CURRENCY
and$fmt->format(123);
to get a currency value with the symbol specified asCURRENCY_SYMBOL
(orINTL_CURRENCY_SYMBOL
)
i.e
function currencyFormatter(float $currency, string $locale, int $fractionDigits): string
{
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
$fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, $fractionDigits);
return preg_replace(
'/[[:^ascii:]]/',
'',
$fmt->format($currency)
);
}
echo currencyFormatter(56868993064.7985, 'de_DE', 2);
//Output: 56.868.993.064,80
Upvotes: 14
Reputation: 1
The formatCurrency()
method is convenient in that it automatically formats the amount according to the rules of the specified currency. For example, when formatting an amount in Euros, the number should show 2 decimal places. But when formatting an amount in Yen, the number should have no decimals as fractions of Yen do not exist (as far as I know). Other currencies may have 3 or even 4 decimals.
If the goal is to keep all functionality of the formatCurrency()
method except for the removal of the currency symbol (or code), then I'd suggest this snippet:
$localeCode = 'en_US';
$currencyCode = 'USD';
$amount = 10000;
$formatter = new \NumberFormatter($localeCode, \NumberFormatter::CURRENCY);
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currencyCode);
$formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, '');
$formatter->format($amount);
Please note that it is necessary to set the currency code BEFORE clearing the currency symbol. Trying to set the currency after clearing the symbol will not work, as setting the currency will automatically load the related symbol.
Upvotes: 0
Reputation: 2184
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;
Upvotes: 13
Reputation: 21396
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
Upvotes: 2