format money in PHP so it is "$xxx.xx USD" - php

I'm trying to use money_format in PHP to get this result: $100,000.00 USD if that is possible.
Here is what I have:
$cost = 100000;
$usd = money_format('%i', $cost);
This is what I get:
USD 100,000.00
What I would like is:
$100,000.00 USD
I may be adding other currencies later, so if the solution breaks the general utility and l10n of money_format then I won't use it and will just use the default value.

i prefer number_format()
$usd = '$'.number_format($number, 2, '.', ',').' USD';

Related

PHP shopping cart calculation

Hi I need to remove 10% from a shopping carts subtotal
Original code:
<?php echo number_format($order->subtotal,2);?>&OID=<?php echo $order->trans_id;?>
I know it's not precise, but would something like this work?
<?php echo number_format($order->subtotal * 0.909090909,2);?>&OID=<?php echo $order->trans_id;?>
Thanks
Use sprintf()
$a = 2324.56*0.909090909 ;
echo sprintf('%0.2f',$a);
output // 2113.24
sprintf() will handle the floating point precession which is the best way to handle.
If needs to display the money format for specific locale it could be doing using money_format
$a = 2324.56*0.909090909 ;
$amount = sprintf('%0.2f',$a);
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#1n', $amount) . "\n";
output // $2,113.24
Here is an explanation on number_format() -ve value precession issue
http://www.howtoforge.com/php_number_format_and_a_problem_with_negative_values_rounded_to_zero
that probably would work, but why not just subtract the 10 percent? If that's the goal, why not just do it? keep in mind number_format rounds up, but I expect that's desired.
<?php echo number_format( ($order->subtotal - ($order->subtotal* .1) ) ,2);?>
This is the math that actually subtracts 10% why not use this instead of something that's close?
try this
$subtotal = $order->subtotal;
$cut_subtotal = $subtotal *(10/100);
$subtotal_new = $subtotal-$cut_subtotal;
Now use this in your code
<?php echo number_format($subtotal_new,2);?>

Use php money format and remove the numbers after the decimal place for GBP

I want to format some numbers from my analytics into currency using the GB money format but as its for a dashboard and doesn't need to be that precise so I want to remove the pence (numbers after the decimal place) and round it which helps with the css layout, how do I do this? Rounding up or down to the nearest pound would be fine.
My code is as follows:
//set locale for currency
setlocale(LC_MONETARY, 'en_GB');
$sales = '8932.83';
echo utf8_encode(money_format('%n', $sales));
This outputs: £8,932.83
However how do I round this to be output as just £8,932 without anything after the decimal place.
I want to use currency format as sometimes the figure is negative in which case money_format returns the number like -£8,932.83 which is preferable to £-8,932 (pound and negative symbol around the wrong way) which is what happened when I formatted using number_format like so:
echo '£'.number_format($sales, 0, '', ',');
Do you want to round it or get rid of the decimals?
To round it which would be 8933 would be:
echo utf8_encode(money_format('%.0n', $sales));
To get rid of the decimals, you could use floor (which rounds down):
echo utf8_encode(money_format('%.0n', floor($sales)));
As the money_format() has been deprecated in PHP 7.4 and removed in PHP 8.0, the suggested function for formatting currency is now NumberFormatter::CURRENCY.
Solving this problem for example would be done this way:
$sales = 8932.83;
$sales = (int)$sales;
$numFormat = new NumberFormatter("en_GB", NumberFormatter::CURRENCY);
$sales = $numFormat->formatCurrency($sales, "EUR");
$sales = str_replace('.00', '', $sales);
echo $sales;

Pricing issues with pounds and pence!

At the moment I store prices for products in the database as a pence number. So 4321 in the database means £43.21.
Then when reading it out, I divide by 100 to get it in pound and pence format.
However, I have a problem.
If the price is 4320, the returned value is 43.2 without the 0.
How can I get around this?
Thanks!
You can format strings with sprintf
See example 9:
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>
<?
echo money_format("%i", 1234.5)
//Output: 1234.50
?>
You can use money_format.
money_format() should do the trick. Alternatively number_format() or the powerful printf().
echo number_format($float, 2, '.', '');
and for pretty printing of large values:
echo number_format($float, 2, '.', ',');

Print Currency Number Format in PHP

I have some price values to display in my page.
I am writing a function which takes the float price and returns the formatted currency val with currency code too..
For example, fnPrice(1001.01) should print $ 1,000.01
The easiest answer is number_format().
echo "$ ".number_format($value, 2);
If you want your application to be able to work with multiple currencies and locale-aware formatting (1.000,00 for some of us Europeans for example), it becomes a bit more complex.
There is money_format() but it doesn't work on Windows and relies on setlocale(), which is rubbish in my opinion, because it requires the installation of (arbitrarily named) locale packages on server side.
If you want to seriously internationalize your application, consider using a full-blown internationalization library like Zend Framework's Zend_Locale and Zend_Currency.
with the intl extension in PHP 5.3+, you can use the NumberFormatter class:
$amount = '12345.67';
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo 'UK: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
$formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo 'DE: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
which prints :
UK: €12,345.67
DE: 12.345,67 €
sprintf() is the PHP function for all sorts of string formatting
http://php.net/manual/en/function.sprintf.php
I use this function:
function formatDollars($dollars){
return '$ '.sprintf('%0.2f', $dollars);
}
I built this little function to automatically format anything into a nice currency format.
function formatDollars($dollars)
{
return "$".number_format(sprintf('%0.2f', preg_replace("/[^0-9.]/", "", $dollars)),2);
}
Edit
It was pointed out that this does not show negative values. I broke it into two lines so it's easier to edit the formatting. Wrap it in parenthesis if it's a negative value:
function formatDollars($dollars)
{
$formatted = "$" . number_format(sprintf('%0.2f', preg_replace("/[^0-9.]/", "", $dollars)), 2);
return $dollars < 0 ? "({$formatted})" : "{$formatted}";
}
Reference Link : https://www.php.net/manual/en/function.number-format.php
$amount = 1235.56
echo number_format($amount, 2, '.', ',');
The output is : 1,235.56
If you don't need comma in output.Please remove comma inside function.
For example
$amount = 1235.56
echo number_format($amount, 2, '.', '');
The output is : 1235.56
From the docs
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
PHP has a function called money_format for doing this. Read about this here.
(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)
$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
output will be
1.234.567,89 €
1.234.567,89 RUR
https://www.php.net/manual/en/numberformatter.formatcurrency.php

How do I format a number to a dollar amount in PHP

How do you convert a number to a string showing dollars and cents?
eg:
123.45 => '$123.45'
123.456 => '$123.46'
123 => '$123.00'
.13 => '$0.13'
.1 => '$0.10'
0 => '$0.00'
If you just want something simple:
'$' . number_format($money, 2);
number_format()
PHP also has money_format().
Here's an example:
echo money_format('$%i', 3.4); // echos '$3.40'
This function actually has tons of options, go to the documentation I linked to to see them.
Note: money_format is undefined in Windows.
UPDATE: Via the PHP manual: https://www.php.net/manual/en/function.money-format.php
WARNING: This function [money_format] has been DEPRECATED as of PHP 7.4.0. Relying on this function is highly discouraged.
Instead, look into NumberFormatter::formatCurrency.
$number = "123.45";
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
return $formatter->formatCurrency($number, 'USD');
i tried money_format() but it didn't work for me at all. then i tried the following one. it worked perfect for me. hopefully it will work in right way for you too.. :)
you should use this one
number_format($money, 2,'.', ',')
it will show money number in terms of money format up to 2 decimal.
In PHP and C++ you can use the printf() function
printf("$%01.2f", $money);
Note that in PHP 7.4, money_format() function is deprecated. It can be replaced by the intl NumberFormatter functionality, just make sure you enable the php-intl extension. It's a small amount of effort and worth it as you get a lot of customizability.
$f = new NumberFormatter("en", NumberFormatter::CURRENCY);
$f->formatCurrency(12345, "USD"); // Outputs "$12,345.00"
The fast way that will still work for 7.4 is as mentioned by Darryl Hein:
'$' . number_format($money, 2);
In php.ini add this (if it is missing):
#windows
extension=php_intl.dll
#linux
extension=php_intl.so
Then do this:
$amount = 123.456;
// for Canadian Dollars
$currency = 'CAD';
// for Canadian English
$locale = 'en_CA';
$fmt = new \NumberFormatter( $locale, \NumberFormatter::CURRENCY );
echo $fmt->formatCurrency($amount, $currency);
/* Just Do the following, */
echo money_format("%(#10n","123.45"); //Output $ 123.45
/* If Negative Number -123.45 */
echo money_format("%(#10n","-123.45"); //Output ($ 123.45)

Categories