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);
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.
NOTE: I've answered my own question below, but posting this anyway for someone else in my situation to learn...
This is driving me insane.
Here's the scenario. There are a million questions here about removing characters like '$' from simple strings (eg. $string = '$10.00') and I can do that just fine.
However... when the string originates from shortcode output (eg. $string = do_shortcode($mycode), I just can't do it. I can remove the 1's and 0's and .'s but the $ won't budge.
Note: The shortcode I'm using is 'Wordpress Currency Switcher' (wpcs_price). My newbie mind tells me that the problem is my string isn't a string at all, but live code or some weird type of array I don't understand.
Example.
function justwork() {
$rawprice = 10.00;
$rawpriceSC = '[wpcs_price value=\''.$rawprice.'\']';
$rawpriceOUT = do_shortcode($rawpriceSC); /* This will output $10.00 */
$finalprice = str_replace('$','',$rawpriceOUT);
echo $finalprice;
}
add_shortcode('justwork', 'justwork');
This will result in:
'$10.00' ($ not removed)
Using the same code on a different character (Eg. the '.') works just fine.
Eg.
$finalprice = str_replace('.','',$rawpriceOUT);
And the output will be:
'$1000'
The $ just won't budge. I've tried substr,trim,preg, lots of other stuff, and I soon realised all is not what it seems, it's carrying some baggage. So I tried capturing the output buffer, and still the resultant string behaved odd.
Any help... oh gosh please, going insane on this one.
ANSWER
While reviewing all the methods I'd looked at, one came up I hadn't.
echo htmlentities($finalprice);
And guess what this output?
<span class="wpcs_price" id="wpcs_58c296db36aa3" data-amount=10 ><span class="wpcs_price_symbol">$</span>10.00</span>
OK. So either I need to use & # 3 6 ; instead of $, or I can operate on that whole mess to get my 10.00. Geeze.
Confirmed that simply replacing the '$' with '& # 3 6 ;' (remove spaces) works fine.
function justwork() {
$rawprice = 10.00;
$rawpriceSC = '[wpcs_price value=\''.$rawprice.'\']';
$rawpriceOUT = do_shortcode($rawpriceSC);
$price = str_replace('$', '', $rawpriceOUT) ;
echo $price;
}
add_shortcode('justwork', 'justwork');
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.
On my website, echo $symbols['currency']; echo $fields['price']; holds a value in US currency format and it outputs R$19800
and echo $symbols['currency']; echo number_format($fields['price']); outputs R$19,800
How do I format it to output R$19.800,00 which is Portuguese price formatting?
I tried
echo $symbols['currency'];
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $fields['price']);
and it outputs R$EUR 19.800,00 which is corrent, but Im finding it hard to remove EUR from printing. Thanks a lot.
Simply use the PHP function str_replace:
$money = money_format('%.2n', $fields['price']);
echo str_replace("EUR", "", $money);
You can do this without locale and str_replace() (which I consider just fixing/hiding something done not correctly) by just using number_format():
echo $symbols['currency'];
echo number_format((float) $fields['price'], 2, ',', '.');
I'm at the moment using the PHP money_format() function, and have money_format('%.0i', $row['price']) at the moment it outputs something like: DKK 199.900 - is there a way I can output it like 199.900 DKK instead?
Use number_format instead (this is cross OS compatible too):
$value = number_format($value, [decimal places]).' DKK';
replace first argument of money_format to '%.0i DKK';
So the pattern money_format('%!.0i DKK', $price); could be a solution where ! will put off auto currency sign and it is added manually at the end
Actually #sandeep was right, he just forgot to add the ! to remove the monetary symbol from the beginning.
setlocale(LC_MONETARY, 'da_DK');
$money_in_the_bank = 9333;
echo money_format('%!.0i DKK', $money_in_the_bank);
This will output:
9.333 DKK