Price formatting for Portuguese - php

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, ',', '.');

Related

Currency symbol is not displaying properly in PHP

Currency symbol is not displaying properly.
Below is my code:
$total = "₮50.40";
echo $total."<br>"; //output: ₮50.40
$str3 = substr($total, 0, 1);
echo $str3; //output: �
The variable $total is displaying correctly. But I extracted the symbol from $total and display it, unfortunately it shows � .
I want to display ₮ from variable $total.
I tried utf8 encoding, but no luck.
The ₮ character is a multi-byte character, so you need to use mb_substr, not substr:
$total = "₮50.40";
$str3 = mb_substr($total, 0, 1);
echo $str3;
Output:
₮
Demo on 3v4l.org

NumberFormatter generate unbreakable space

I try to put a formated currency input from php as value of an input,
$format = numfmt_create( 'fr_FR', NumberFormatter::CURRENCY);
$datavalue = numfmt_format_currency($format, $data['value'], 'EUR');
It works, but the result contain unbreakable space caracter like so
value='10,0 €'
That lead to mess up with my design, I didn't found how to remove it yet
Already tried :
str_replace(' ', " ", $datavalue);
Run the result through html_entity_decode() then perform your str_replace(' ', '', $datavalue);
That class/function prefers an integer be passed to it, so you need to clean up your data before you ever get to the function.
You can use numberFormatter
example:
$amount = '12345.67';
$formatter = new NumberFormatter('fr_FR', NumberFormatter::CURRENCY);
echo 'format: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
//output
12 345,67 €
edit: other method is use this MoneyFormat
example
$number = 1234.56;
// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

Negative Currency Display

I have this code
echo money_format('£%i', $PL);
But for negative output the display is
£-1.00
I want
-£1.00
How can I get the - sign before the pound sign?
Thanks.
setlocale(LC_MONETARY, 'en_GB');
echo money_format('%+n', -123.45); // -£123.45
If you can not see the symbol correctly, try using:
setlocale(LC_MONETARY, 'en_GB.UTF-8');
echo money_format('%+n', -123.45);
http://php.net/manual/en/function.money-format.php

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?
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.

PHP money_format for EURO

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);

Categories