NumberFormatter gets Hungarian currency format wrong - php

If I use the money_format function, it prints the currency amount in the Hungarian format correctly:
$ php -a
Interactive mode enabled
php > setlocale(LC_MONETARY, 'hu_HU');
php > $number = 1234.5672;
php > echo money_format('%n', $number)."\n";
1.234,57 Ft
But if I try with the NumberFormatter class it returns the wrong format
$ php -a
Interactive mode enabled
php > $f = new NumberFormatter('hu_HU', NumberFormatter::CURRENCY);
php > $f->setAttribute($f::FRACTION_DIGITS, 2);
php > echo $f->formatCurrency('1234.5672', 'HUF')."\n";
1 234,57 Ft
I think it's a bug.

It is depend on Which operating System used. it will differ based on Windows and Linux.
Some time it doesn't work in windows

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.

Why PHP COM objects are not returning correct results?

I'm trying to access Windows SAPI5 or Text to speech (TTS) using PHP. The standard approach is to create a COM object for "SAPI.SpVoice", then get the installed voices.
Sample PHP code:
<?php
$obj = new COM('SAPI.SpVoice');
$voices = $obj->GetVoices;
$count = $voices->Count;
print $count; #prints "1"
Unfortunately the output returned from PHP's COM object is incorrect because I have 5 voices installed on my system, but PHP only returns 1.
So, just to check if this a PHP specific issue, I wrote the same code in Perl 5.8 (strawberry).
Sample Perl code:
#!/usr/bin/perl
use Win32::OLE;
my $obj = Win32::OLE->new('SAPI.SpVoice');
my $voices = $obj->GetVoices;
my $count = $voices->Count;
print $count; #print "5" which is correct.
So the perl code correctly returns that I have 5 TTS voices on my system, but PHP returns only 1?
Is this a bug or am I doing something wrong? What could be the possible cause of this?
P.S. I've tried this on two different computers and results are the same.
I figured this after some trial error. It looks like if I use the 32-bit version of PHP then I get the correct results (5 voices). But since I had installed the 64-bit version by default I only get 1 voice.
I think the TTS voices are mostly 32 bit (like those installed on my system) and so when running with a 64-bit php.exe it only returns 64-bit voices. With 32 php.exe it returns all voices.
Posting this as answer in case someone faces a similar issue in future.

NumberFormatter::formatCurrency is not showing a currency symbol

I'm using NumberFormatter::formatCurrency to display formatted currency values, like this;
$value = 395;
$fmt = numfmt_create('en_GB', NumberFormatter::CURRENCY);
echo numfmt_format_currency($fmt, $value, 'gbp');
On my Windows dev box, and Centos UAT box, this outputs the desired £395.
But on the production Centos box, it outputs gbp395.
Any idea what is missing? I have checked intl extension is enabled.
Is there something wrong with my locale files perhaps? When I type
locale -a
in command line, I get a long list of locales, of which en_GB is one.
Maybe en_GB is not a valid locale on your system. Try en_GB.UTF-8 or en_GB.ISO-8559-1 f.e.
The NumberFormatter class is incorrect and the GBP needs to be in capital like so:'GBP'
Try this:
$value = 395;
$currencyFormat = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo $currencyFormat->formatCurrency($value, 'GBP');
This will render £395.00

module of a too long var in php

How can i do to the module of a var that contents 24 digits in php
00120345030000067890142807 % 97
the result of this operation must be 1 but the problem is that the var that contains de value is too long.
You can use the BCMath PHP functions to handle large numbers. Depending on your environment, the BCMath extension is often already bundled with PHP, you might be able to search for it in your package manager of choice if you're using Linux. If you're on an older version of PHP, let me know and I can hopefully show you how to compile the extension manually from the php source tree.
The function to use is bcmod - http://www.php.net/manual/en/function.bcmod.php
You can use it like this:
<?php
$bigNumber = '00120345030000067890142807';
echo($bigNumber % 97 . "\n");
echo(bcmod($bigNumber, 97) . "\n");
If you run this, you'll see it outputs the expected result and that the standard mod doesn't:
$ php -q test.php
65
1

PHP exec change encoding

I need to address UTF-8 filenames with the php exec command. The problem is that the php exec command does not seem to understand utf-8. I use something like this:
echo exec('locale charmap');
returns ANSI_X3.4-1968
looking at this SO question, the solution lookes like that:
echo exec('LANG=de_DE.utf8; locale charmap');
But I still get the same output: ANSI_X3.4-1968
On the other hand - if I execute this php command on the bash command line:
php -r "echo exec('LANG=de_DE.UTF8 locale charmap');"
The output is UTF-8.
So the questions are:
Why is there an different result be executing the php command at bash and at apache_module/web page?
How to set UTF-8 for exec if it runs inside a website as apache module?
To answer my own question - i found the following solution:
setting the locale environment variable with PHP
$locale='de_DE.UTF-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);
echo exec('locale charmap');
This sets to / returns UTF-8. So i'm able to pass special characters and umlauts to linux shell commands.
This solves it for me (source: this comment here):
<?php
putenv('LANG=en_US.UTF-8');
$command = escapeshellcmd('python3 myscript.py');
$output = shell_exec($command);
echo $output;
?>
I had the similar problem. My program was returning me some German letters like: üäöß. Here is my code:
$programResult = shell_exec('my script');
Variable $programResult is containing German umlauts, but they were badly encoded. In order to encode it properly you can call utf8_encode() function.
$programResult = shell_exec('my script');
$programResult = utf8_encode($programResult);

Categories