I can't seem to find anything in the php.net documentation that explains the following results:
$ php -r 'var_dump(bcsub("0.3", "0.2", 4));'
string(6) "0.1000"
$ php -r 'var_dump(bcmul("0.3", "0.2", 4));'
string(4) "0.06"
The subtraction result is exactly what I would expect it to be (I specified a 4-digit scale, and it gave me one in the result). The multiplication result does not (I specified a 4-digit scale, but it gave me a 2-digit one in the result). Why the difference?
Note: I already know how to use number_format()
, and I also know that 0.06 === 0.0600
mathematically. I'm only interested in understanding why BC Math seems to act differently with respect to the scale of the result.
Note #2: As mentioned above, number_format()
is not an answer to this question, and the answers used in the referenced "duplicate question" all advise using number_format()
. I know full well that this function can be used to format the number to the specified precision. I'm just curious to know WHY the return values for these functions have different scales, NOT how to fix them so that they do.