I would like to have the country code display for all currencies when they are formatted. At the moment I have:
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(-10, 'USD') . PHP_EOL; //-$10.00
echo $formatter->formatCurrency(-10, 'CAD') . PHP_EOL; //-CA$10.00
How can I make the USD currency display either -US$10,00 or -USD$10.00?
Thanks
Related
I'm using this piece of code to format numbers from my database so it becomes currency
$formnum = numfmt_create( 'fr_FR', NumberFormatter::CURRENCY);
$datavalue = !empty($datavalue) ?
numfmt_format_currency($formnum, intval($datavalue), 'EUR') : '';
but if $datavalue is equal to (let say) 10, I'll get 10,00 €
But obviously what I wanted is 10 €
(if you want to try it)
$formnum = numfmt_create( 'fr_FR', NumberFormatter::CURRENCY);
$datavalue = numfmt_format_currency($formnum,intval("10"), 'EUR');
echo $datavalue;
Try something like this, using the full scope of the NumberFormatter class (namely, the FRACTION_DIGITS attribute):
$fmt = new NumberFormatter('fr_FR', NumberFormatter::CURRENCY);
$fmt->setTextAttribute(NumberFormatter::CURRENCY_CODE, 'EUR');
$fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
echo $fmt->formatCurrency($datavalue, 'EUR');
Docs are here: http://php.net/manual/en/numberformatter.formatcurrency.php
How to format number = "$lowprice" to currency
$price_content .= '<span>';
if ($currency_sympol) $price_content .= get_post_meta($id,$wprs_prefix.'lowprice', TRUE);
$price_content .= '<sup>'.$currency_sympol.'</sup>';
$price_content .= '</span>';
To format a number as currency use the money_format() function. (Docs)
$lowprice = 1234.56;
// Italian national format with 2 decimals
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $lowprice) . "\n";
// Eu 1.234,56
Since the currency is derived from the server locale, you might need to change it (see sample).
I'm building an personal app where I can follow my cryptocurrency coins which I'm currently holding and following. The problem I've noticed are the decimals behind the price. A currency normally has max 2 decimals, but the API of Coinmarketcap gives me more depending on how much the price is.
Below is an example of the value I get from the API, and how I actually want the price to be shown. Values above 1000 will get a comma and no decimals.
$950194.0 -> $950,194
$81851.6 -> $81,852
$4364.97 -> $4,365
$326.024 -> $326.02
$35.0208 -> $35.02
$4.50548 -> $4.51
$0.0547128 -> $0.0547128
I've never tried something ever like this before, so I really don't know how to start. Tried using round() and numberFormat(), but couldn't make the same like how I wanted above in the example.
You can use money_format to make thing easier. However, the problem is about what precision do you want. You have to figure it out yourself since I cannot find the pattern from your examples. Yet I wrote the simple function for you with round and money_format. What the rest is adjust the precision to the point where you want in each case.
<?php
function my_money_format($number, $precision=0)
{
$number = preg_replace( '/[^0-9.,]/', '', $number); // clean the input
$number = round($number, $precision);
$format = '%.' . $precision . 'n';
setlocale(LC_MONETARY, 'en_US'); // set money format to US to use $
return money_format($format, $number);
}
echo my_money_format('$950194.0', 0); // $950,194
echo "\n";
echo my_money_format('$81851.6', 0); // $81,852
echo "\n";
echo my_money_format('$4364.97', 0); // $4,365
echo "\n";
echo my_money_format('$326.024', 2); // $326.02
echo "\n";
echo my_money_format('$35.0208', 2); // $35.02
echo "\n";
echo my_money_format('$4.50548', 2); // $4.51
echo "\n";
echo my_money_format('$0.0547128', 7); // $0.0547128
echo "\n";
Am fetching a price from my database. It is being displayed using.
It display the prices as follows: 5000000
How can i split it to display something like 5,000,000
have you tried the manual?
number_format does exactly what you want:
<?php
$number = 5000000;
echo number_format($number); //output: 5,000,000
use money_format(string,number)
(Money format not fit for windows)
money_format
also use `number_format()
number_format — Format a number with grouped thousands
<?php echo number_format("5000000")."<br>"; ?>
number_format
You need to use the php built in function number_format for this
echo number_format($price);
<?php
$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
// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56
// Using a negative number
$number = -1234.5672;
// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
// ($ 1,234.57)
// Similar format as above, adding the use of 2 digits of right
// precision and '*' as a fill character
echo money_format('%=*(#10.2n', $number) . "\n";
// ($********1,234.57)
// Let's justify to the left, with 14 positions of width, 8 digits of
// left precision, 2 of right precision, withouth grouping character
// and using the international format for the de_DE locale.
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
// Eu 1234,56****
// Let's add some blurb before and after the conversion specification
setlocale(LC_MONETARY, 'en_GB');
$fmt = 'The final value is %i (after a 10%% discount)';
echo money_format($fmt, 1234.56) . "\n";
// The final value is GBP 1,234.56 (after a 10% discount)
?>
I am customizing k2 store front end and I need some help with currency formatting.
My code is:
<span>Tax: <?php echo $item->tax ?></span>
<span>Price without tax: <?php echo $item->price; ?></span>
Don't know why but this code outputs:
Tax: 11
Price without TAX: 50.00000
I would like to output:
Tax: 11,00 EUR
Price without tax: 50,00 EUR
I found some hints for formatting values in PHP but I was not able to implement it in my case. I have a poor PHP knowledge.
how about using php's money_format function? In this case a similar SO post seems to have an answer
setlocale(LC_MONETARY, 'nl_NL.UTF-8');
$amount = money_format('%(#1n', $amount);
echo $amount;
you could put this into a function like
function to_euro($input){
setlocale(LC_MONETARY, 'nl_NL.UTF-8');
return money_format('%(#1n', $input);
}
then use it like
echo to_euro($item->tax);