chris pursey - 7 months ago 82

PHP Question

I am having a problem with the multiplication of two decimals 30.63 and 0.15. My calculator says that it should result in 4.60.

$commission = bcmul(30.63, 0.15,2);

Result from this is 4.59.

From what I had read bcmul was meant to work with decimal numbers?

Many thanks,

Answer

From the documentation for the `$scale`

parameter to `bcmul`

:

This optional parameter is used to set the number of digits after the decimal place in the result.

This essentially means that the number will be rounded **down** (or up for a negative number) when it's used, e.g.

```
> echo bcmul(0.99, 1, 1);
0.9
```

For your values, 30.63 * 0.15 is equal to 4.5945. Supplying a `$scale`

value of 2 means that you get 4.59, as you've reported.

I'm not sure why you're expecting 4.60, unless you're specifically expecting the result to be rounded **up**.