0

Take a look at this example in regular math:

<?php
$rate   =   788.159;
$amount =   1182.2385;

var_dump($amount*(1/$rate));

and the answer is as expected:

float(1.5)

however when same is done in BCMath:

<?php
bcscale(12);

$rate   =   "788.159";
$amount =   "1182.2385";

var_dump(bcmul($amount, bcdiv("1", $rate)));

the answer is always:

string(14) "1.499999999999"

While it should be precise "1.5"!

I have tried increase default scale value to 100, 200, 1000. I have tried increasing scale specifically for my bcdiv call, but it doesn't want to give 1.5 as an answer in anyway.

Any help will be appreciated

M. Furqan
  • 31
  • 3
  • `bc` funcs return a string. `echo serialize($amount*(1/$rate));` – AbraCadaver Jan 12 '17 at 21:26
  • So there is no better way but to do `round(floatval($result), 8);` (assume $result is my bcmul calculation and I want to focus on first 8 digits) ... in PHP7 with strict types, converting string to float just to round it, doesn't sound like a good idea. Is it? – M. Furqan Jan 12 '17 at 21:50

0 Answers0