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
Related
I am trying to get number value with plus and minus
<?php
$num1= '-12.20000';
$num2= '+18.20000';
echo rtrim(str_replace('.00', '', number_format($num1, 2));
echo rtrim(str_replace('.00', '', number_format($num2, 2));
?>
Need output like
-12.2
+18.2
I can't see exactly what you need. There are not enough examples and your description of the task is not sufficient.
The number is formatted with a sign and 2 decimal places. If the last digit is a 0, it is removed with preg_replace().
$data = ['-12.20000','+18.20000', 234.0, 2.1234];
foreach($data as $value){
$formatVal = sprintf("%+0.2f",$value);
$formatVal = preg_replace('~(\.\d)0$~','$1',$formatVal);
echo $value.' -> '.$formatVal."<br>\n";
}
Output:
-12.20000 -> -12.2
+18.20000 -> +18.2
234 -> +234.0
2.1234 -> +2.12
If the result is only ever required with one decimal place, you can use
$formatVal = sprintf("%+0.1f",$value);
without the preg_replace.
In python, you could do it like this:
def returnWithSign(str):
n = float(str)
if n>0:
return '+{}'.format(n)
return n
Instead of using difficult functions in PHP just use the native stuff PHP brings with it. One fantastic thing of that stuff is the NumberFormatter class.
$formatter = new NumberFormatter( 'en_GB', NumberFormatter::DECIMAL );
$formatter->setTextAttribute(NumberFormatter::POSITIVE_PREFIX, '+');
$num1= '-12.20000';
$num2= '+18.20000';
echo $formatter->format($num1) . PHP_EOL;
echo $formatter->format($num2) . PHP_EOL;
Exactly does what you want.
Output: https://3v4l.org/UQX3Y
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
So I have this little problem.
I have a form where you can enter a number, it can be in European format, with comma as decimal (,), or American with the decimal point (.).
Knowing this, how can I convert the number format from the European American format? And the American format leave it unchanged?
I tried using this formula but the result is wrong.
$american = '12.5';
$european = '12,5';
$locale = 'it_IT.utf8';
setlocale ('LC_NUMERIC', $locale);
$fmt = new NumberFormatter ($locale, NumberFormatter::DECIMAL);
$american = $ fmt->parse($american);
$european = $ fmt->parse($europen);
response
$american = 125
$european = 12.5
Where is the problem?
php ver. 5.4.10
try following code:
$american = '12.5';
$european = '12,5';
$locale = 'en_US.utf8';
$fmt = new NumberFormatter ($locale, NumberFormatter::DECIMAL);
$american = $fmt->parse($american);
$locale = 'it_IT.utf8';
$fmt = new NumberFormatter ($locale, NumberFormatter::DECIMAL);
$european = $fmt->parse($european);
Get the desired result?
Isn't better to use str_replace() instead?
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 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)