I'm adding a price value, retrived from a db, inside a cell on a pdf created with FPDF, using this function:
$fmt = numfmt_create( 'it_IT', NumberFormatter::CURRENCY );
numfmt_format_currency($fmt, $row['prezzo'], "EUR")
Considering that the EUR symbol would be output like this:
 €
I've tried to define a EURO costant:
define('EURO',chr(128));
but i can't use it with NumberFormatter.
How can i print the € symbol correctly in the pdf?
Related
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?
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;
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
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
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.
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?
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;
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
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
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.
This code:
$formatter = new \NumberFormatter('ar', \NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(1234.99, 'EUR'), PHP_EOL;
outputs:
€ ١٬٢٣٤٫٩٩
Is there any way to make formatCurrenct to output
EUR ١٬٢٣٤٫٩٩
and still would be nice if ISO symbols would be translated.. so if i pass YER as a currency, I want to get something like this:
ر.ي. ١٬٢٣٥
I ran into some problems using the php NumberFormatter class. I want rounded digits to be displayed without any decimals, and digits which has decimals to be rounded with 2 decimals. i.e.
$fmt = new NumberFormatter('nl', NumberFormatter::CURRENCY);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2);
var_dump($fmt->formatCurrency(15.15, 'EUR')); //Outputs € 15,15
var_dump($fmt->formatCurrency(15, 'EUR')); //Outputs € 15,00
As you can see the last dump is outputting 15,00 but I'm expecting 15 here.
Whenever I use the "full" locale nl_NL instead of nl the formatter behaves as expected, but I'm unable to use that because I only have the language available in my locale.
$fmt = new NumberFormatter('nl_NL', NumberFormatter::CURRENCY);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2);
var_dump($fmt->formatCurrency(15.15, 'EUR')); //Outputs € 15,15
var_dump($fmt->formatCurrency(15, 'EUR')); //Output € 15
This seems like a bug in the NumberFormatter but I really have no clue how to fix it or figure out some other workaround. I'd be very grateful for any help.
Dirty fix:
$formattedCurrency = preg_replace('/,00$/', '', $fmt->formatCurrency(15, 'EUR'));
you can use the MAX_SIGNIFICANT_DIGITS attribute:
$fmt = new NumberFormatter('nl', NumberFormatter::CURRENCY);
$fmt->setAttribute(\NumberFormatter::MAX_SIGNIFICANT_DIGITS, 7);
var_dump($fmt->formatCurrency(15.15, 'EUR')); // Outputs € 15,15
var_dump($fmt->formatCurrency(15, 'EUR')); // Outputs € 15
We can use following code for get $ mark and format money.
setlocale(LC_MONETARY, 'en_US.UTF-8');
$amount = money_format('%(#1n', $amount);
How to get euro symbol from php money_format?
i think this will work
setlocale(LC_MONETARY, 'nl_NL.UTF-8');
$amount = money_format('%(#1n', 20);
echo $amount;
Warning :-
This function has been DEPRECATED as of PHP 7.4.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.
Use this
setlocale(LC_MONETARY, 'nl_NL');
$amount = money_format('%(#1n', $amount);
$price = 880000;
echo number_format($price, 2, ',', '.');
input => 880000
output => 880.000,00
A quite old thread but if some google here
That worked even on a local MAMP
setlocale(LC_MONETARY, 'de_DE');
echo money_format('€ %!n', 1.620.56);
// € 1.620.56
You can use the HTML entity
"&euro";
Directly in your code
$amount = money_format('%(#1n', $amount) .htmlentities('€');
EDIT
Or you can use the ! flag %(#!1n'
so you code will looks like
$amount = money_format('%(#!1n', $amount) .htmlentities('€');
You can see the following post
PHP show money_format without currency text
Hope this would help
Finally a Italy locale worked for me.
$amount = '1600.00';
setlocale(LC_MONETARY, 'it_IT.UTF-8');
$amount = money_format('%.2n', $amount);
echo str_replace('Eu','€',$amount);
This is an old thread but I felt it worth sharing a relevant solution as the top answer doesn't provide an actual solution. I found this whilst searching for Spanish LC_monetary so if, like me, you end up here you know have an answer.
I use the following wrapped in a function with the advantage that it handles zeros nicely (see example in calculations ). I use this in my calculators for a slicker accounting layout. This example is Spain but you can use whichever Euro area you prefer:
setlocale(LC_MONETARY, "en_ES");
function C_S($iv) {
if(in_array($iv, array(' ','',0)) ){return'<i>€</i>0.00';}
else{return str_replace('EU','<i>€</i>', money_format("%i", $iv));}
}
the italics are not necessary, I use them with css for alignment for finance style reports. Again, here for info.
to use:
echo C_S(1234);