Bcmul Reports 0

I have a simple code as shown below.

$amount = 447274.44882;
$rate = 0.00001;

echo floatNumber(bcmul($amount, $rate, 8), 8);

This produces 0.00000000 if it should be 4.47274449. If I change the speed to 0.0001, then it will output the correct number, something larger than 4 decimal places, and will report 0.

Am I doing something wrong or is this a known limitation or something else? Seems pretty big if this is the case.

+3
source share
1 answer

If you added 0.00001to the string using the default settings (and what happens if you feed bcmulusing float, as it expects the string ) you will get the following:

var_dump( (string)0.00001 );
string(6) "1.0E-5"

This is not explicitly documented, but the bcmath functions seem to return are set to zero when they encounter invalid input:

var_dump( bcadd('Hello', 'world!', 8) );
var_dump( bcadd('33', 'Foo', 8) );
var_dump( bcdiv('33', 'Foo', 8) );
string(10) "0.00000000"
string(11) "33.00000000"
Warning: bcdiv(): Division by zero
NULL

, 2 . , :

var_dump( bcmul('447274.44882', '0.00001', 8) );
string(10) "4.47274448"

100- , . , - :

var_dump( bcmul('20.01', '1.444', 3) );
var_dump( bcmul('20.01', '1.444', 2) );
var_dump( bcmul('20.01', '1.444', 1) );
var_dump( bcmul('20.01', '1.444', 0) );
string(6) "28.894"
string(5) "28.89"
string(4) "28.8"
string(2) "28"
+2

All Articles