1
When executing:
$number = 50/3;
var_dump(number_format($number, 2, '.', ''));
is returned
string(5) "16.67"
however I need this value to be given in decimal type, so I run the cast to (double).
However, when performing the cast:
$num = (double)number_format($number, 2, '.', '');
var_dump($num);
php returns me:
float(16.666666666667)
Thus, it disregards function formatting
number_format
Remember that this happens on the production server. On the development server, this cast (double) respects number_format.
I’ve replicated the same production data on the development server, but to no avail.
Production version of PHP: PHP 7.2.15-1+0~20190209065041.16+jessie~1.gbp3ad8c0
PHP Version in Development: PHP 7.0.33-0+deb9u1
Any suggestions?
If you don’t cast to double after number_format the value gets the right number of decimals in production?
– fajuchem
Yes. I did the test in Production: I removed the cast for double and it got the correct number of decimals.
– WebCraft
Face really ta strange this ae, I tested site and works normal, who knows you try to use something else instead of number_format, this here should bring the same result. var_dump(float) sprintf('%0.2f', $number));
– fajuchem
I get it. I sincerely believe there is some bug in the PHP version I’m using in production. In Development (Debian 9) we don’t face this problem. But in Production (Debian 8 Jessie) is the first time I face something like this. Anything we do will migrate the production server system to a newer version of Debian, or update PHP
– WebCraft
I tested in this environment: PHP 7.2.15-1+ubuntu18.04.1+deb.sury.org+1, and it worked normal.
– fajuchem
But another thing you can still try is to use round($number, 2).
– fajuchem
I tested round($number, 2) and persists.
– WebCraft
ok, just one more thing then, before calling number_format put it here, ini_set('Precision', 2);
– fajuchem