chris pursey chris pursey - 1 year ago 151
PHP Question

Decimal multiplication in php

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 Source

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);

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download