Writing the good format of a currency in PHP - php

When we write an invoice, we have to respect the money format
For example :
in France, you will write 1000,00 €
In the USA, $ 1,000.00
I would like to know if it is handled by some PHP library ? especially the money symbol at the left or right.
Edit :
I have never been downvoted like this and i think my question wasn't that well asked. Sorry for that.
I already know different formatting functions in PHP and I understand that the formatter options should be selected for each country and their money format. Thou, i don't have the time to do that job.
My objective is to format any money values for all possible locales in the world without registering all those locales.
Maybe somebody wrote a class which can do the job but I didn't find it.
Btw I know it is difficult, I can for example talk of the example of the EUR.
In many contry they write EUR xxxxx. In some countries, it is written xxxx EUR.

Yep, by the intl module.
For currencies there is a NumberFormater class:
http://www.php.net/manual/en/numberformatter.formatcurrency.php

http://ru2.php.net/number_format will help you.
You can simply setup it by yourself using number_format(). Save rule for each currency in db then apply on view regarding it's type.

The basic PHP function money_format can handle your output quite well.
If you need more, check out the Money PHP Library. IT is very powerfull.
From the Documentation:
<?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)

Related

How to set currency to setlocale / money_format in PHP

setlocale is taking a country and a language as parameters.
'money_format' is taking amount and other params.
But how can I tell a currency to PHP?
What if I want to use EUR in Australia?
like javascript can:
var formatter = new Intl.NumberFormat('en-AU', {
style: 'currency',
currency: 'EUR',
});
formatter.format(2500);
Chrome obviously understands that it is a foreign currency that need to be formatted to international currency format like so "EUR 2,500.00".
IE does not understand it, but it offers something "€2,500.00"
PHP (that is how I use it)
setlocale(LC_MONETARY, 'en-AU');
return money_format('%.2n', 2500);
gives "$2,500.00" (I know it is Ubuntu should be blamed)
It takes some default currency for Australia and I cannot find a way to change it. Is there a formatting library or something that I'm missing that can help?
Java script does not require installing locales individually, like Ubuntu does.
Maybe should we rely on Browsers in this case?
You can use the NumberFormatter in the intl extension instead.
$fmt = new NumberFormatter('en_AU.UTF8', NumberFormatter::CURRENCY);
print $fmt->formatCurrency(2500, 'EUR');
And it doesn't require to install every locale individually, since it ships with its own locale data.

PHP money_format not displaying currency symbol

I'm trying to use money_format() to obviously add separators and a currency symbol to a number I have. I've seen plenty of examples where they work, but for some reason the currency symbol does not display.
$_price = '10995';
setlocale(LC_MONETARY, 'en_GB.UTF-8');
echo money_format('%n', $_price);
results in:
10995.00
I can do this and it nearly works, however as I understand, that's not the point?:
echo money_format('£%n', $_price);
results in:
£10995.00
I can see that this also formats the number close to how I want, without the .00:
echo '£'.number_format( $_price );
results in:
£10,995
From Sean's link in the above comment, I can see that the locale for en_GB was not created.
once created with sudo locale-gen en_GB.UTF-8 I had the currency symbol

NumberFormatter::formatCurrency() ignores MIN_FRACTION_DIGITS

I want to use PHP's Intl's NumberFormatter class to display prices in a human-readable format. What our project needs:
The CLDR number pattern, and the currency and separator symbols will need to be configured through our code and not default to what Intl/ICU knows.
Our application will take care of the decimals. NumberFormatter should display any decimals that we pass on to it.
However, when playing around with different configurations to find the exact combination that works for our project, I noticed some effects that I can't explain. The three formatters in the following code snippet are almost identical. As opposed to the first one, the second one uses the euro instead of the U.S. dollar, and the third one has a currency sign set. The output of the first formatter is as I expected it to be, but when I change the currency or set a currency sign, the MIN_FRACTION_DIGITS attribute is ignored and the sign is never changed.
<?php
$fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 4);
echo $fmt->formatCurrency(1234567890.891234567890000, "EUR")."\n";
// Outputs 1.234.567.890,8912 €
$fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 4);
echo $fmt->formatCurrency(1234567890.891234567890000, "USD")."\n";
// Ouputs 1.234.567.890,89 $
$fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 4);
$fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, '%');
echo $fmt->formatCurrency(1234567890.891234567890000, "EUR")."\n";
// Outputs 1.234.567.890,89 €
?>
The first table row under General Purpose Numbers of the Unicode CLDR number pattern documentation describes that when parsing currency patterns, the two zeroes in the decimal part of the pattern will need to be replaced by however many digits the application thinks is appropriate. The application here is ICU (the C library that PHP uses for this), and the MIN_FRACTION_DIGITS attribute does its job of letting me override default behavior in the first example, but not in the second or the third.
Can someone please explain this seemingly random change in behavior? Let me know if there is any additional information that you need.
I just found the following:
https://bugs.php.net/bug.php?id=63140
http://bugs.icu-project.org/trac/ticket/7667
[2012-10-05 08:21 UTC] jpauli#email.com
I confirm this is an ICU bug in 4.4.x branch.
Consider upgrading libicu, 4.8.x gives correct result

Gettext() with larger texts

I'm using gettext() to translate some of my texts in my website. Mostly these are short texts/buttons like "Back", "Name",...
// I18N support information here
$language = "en_US";
putenv("LANG=$language");
setlocale(LC_ALL, $language);
// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, "/opt/www/abc/web/www/lcl");
textdomain($domain);
echo gettext("Back");
My question is, how 'long' can this text (id) be in the echo gettext("") part ?
Is it slowing down the process for long texts? Or does it work just fine too? Like this for example:
echo _("LZ adfadffs is a VVV contributor who writes a weekly column for Cv00m. The former Hechinger Institute Fellow has had his commentary recognized by the Online News Association, the National Association of Black Journalists and the National ");
The official gettext documentation merely has this advice:
Translatable strings should be limited to one paragraph; don't let a single message be longer than ten lines. The reason is that when the translatable string changes, the translator is faced with the task of updating the entire translated string. Maybe only a single word will have changed in the English string, but the translator doesn't see that (with the current translation tools), therefore she has to proofread the entire message.
There's no official limitation on the length of strings, and they can obviously exceed at least "one paragraph/10 lines".
There should be virtually no measurable performance penalty for long strings.
gettext effectively has a limit of 4096 chars on the length of strings.
When you pass this limit you get a warning:
Warning: gettext(): msgid passed too long in %s on line %d
and returns you bool(false) instead of the text.
Source:
PHP Interpreter repository - The real fix for the gettext overflow bug
function gettext http://www.php.net/manual/en/function.gettext.php
it's defined as a string input so your machines memory would be the limiting factor.
try to benchmark it with microtime or better with xdebug if you have it on your development machine.

PHP numbers to words SPANISH!

Does anyone know of a free-licence PHP code that would convert numbers to words in spanish?
It's needed for my work for generating bills so it needs to be accurate.
My knowledge of spanish is practically non-existent, so it would probably be hard to write it myself; I don't know anything about spanish grammar.
Edit: I've written my own version of this, for now it works for only 3 digit numbers (on both sides of the decimal symbol), but it's a good start. If I ever get it big (5 languages planned atm), I'll probably share it on github. Don't bet on it though.
You may use PHP Intl extension to do that.
if (version_compare(PHP_VERSION, '5.3.0', '<')
|| !class_exists('NumberFormatter')) {
exit('You need PHP 5.3 or above, and php_intl extension');
}
$formatter = new \NumberFormatter('es', \NumberFormatter::SPELLOUT);
echo $formatter->format(1234567) . "\n";
Output:
un millón doscientos treinta y cuatro mil quinientos sesenta y siete
It works not only with Spanish but many other languages as well.
Is there an easy way to convert a number to a word in PHP?
From the above, you can derive the "word" from the number, and then translate it to any language you like using any of the Translate API's out there ... or your own lookups.
Edit
Another way you could employ is simply hard coding in a PHP file or a text file a big array of values:
$numTranslation = array(1 => "uno", 2 => "dos");
Then, in your code, just retrieve the echo $numTranslation[2] if the number was 2 to print out the spanish equivalent.
Edit 2
Just to make it a bit more complete, if you ever want to support multiple languages, and not just spanish:
$numTranslation = array(
1 => array("en" => "One", "es" => "uno"),
2 => array("en" => "Two", "es" => "dos")
);
And to print it out to the end user: echo $numTranslation[2]['es']; to get the Spanish equivalent...
The only thing I could find is a Perl script.
The code itself is easy to write in PHP, and from the Perl script you can get the logic and the Spanish words for numbers.
here's the words...
1 to 100
http://www.top-tour-of-spain.com/1-100-Numbers-In-Spanish.html
100+
http://www.intro2spanish.com/vocabulary/numbers/advanced.htm
or
http://www.donquijote.org/spanishlanguage/numbers/numbers2.asp
and I found some nicely formatted VBA here:
http://www.excelforum.com/excel-programming/637231-translating-numbers-into-words.html

Categories