php-src icon indicating copy to clipboard operation
php-src copied to clipboard

locale + bcmath = "function argument is not well-formed"

Open Kadigan opened this issue 4 years ago • 4 comments

Description

The following code:

<?php
echo "---------------------------------------\n";
var_dump(setlocale(LC_ALL, 0));

echo "RES: ".bcmul(10.0, 1.23, 2)."\n";

echo "---------------------------------------\n";
if ( false === setlocale(LC_ALL, 'pl_PL', 'pl', 'Polish_Poland.28592', 'pl', 'pl') )
    echo "(failed to set pl_PL)\n";

var_dump(setlocale(LC_ALL, 0));

echo "RES: ".bcmul(10.0, 1.23, 2)."\n";

Resulted in this output:

---------------------------------------
string(21) "C/en_US.UTF-8/C/C/C/C"
RES: 12.30
---------------------------------------
string(5) "pl_PL"
PHP Warning:  bcmul(): bcmath function argument is not well-formed in /Volumes/HTDOCS/locale.php on line 13

Warning: bcmul(): bcmath function argument is not well-formed in /Volumes/HTDOCS/locale.php on line 13
RES: 0.00

But I expected (a variation of) this output instead:

---------------------------------------
string(1) "C/en_US.UTF-8/C/C/C/C"
RES: 12.30
---------------------------------------
string(2) "pl"
RES: 12.30

PHP Version

PHP 7.4.4

Operating System

macOS 10.13.6

Comment

It looks like in some configurations (at least one on a shared config we send websites to), internal float representations get localized to comma-separated decimals.

Now, either bcmath doesn't understand "10,0" or it shouldn't try to convert to "10,0" when bcmath expects "10.0", regardless of locale.

Kadigan avatar Feb 24 '22 13:02 Kadigan

internal float representations get localized to comma-separated decimals.

Right, but that has changed as of PHP 8.0.0, and PHP-7.4 won't get regular bug fixes anyway, so you need to work around this issue until you can upgrade to PHP 8.

cmb69 avatar Feb 24 '22 13:02 cmb69

I just verified that this is indeed fixed in PHP 8. Thank you.

Kadigan avatar Feb 24 '22 13:02 Kadigan

As work-around, either pass strings instead of floats (if necessary, convert floats to strings with something like number_format($float, bcscale(), ".", "")). Also note that very large floats can still cause issues under PHP 8, see https://bugs.php.net/bug.php?id=66959.

cmb69 avatar Feb 24 '22 13:02 cmb69

Yeah, I found this on Tuesday probably, and I've already "armed" everything bcmath with an sprintf("%.nF", ....

Thank you for your input!

Kadigan avatar Feb 24 '22 14:02 Kadigan