Reputation: 1
We all know that dealing with float point numbers may meet troubles like this:
echo intval(0.58*100);//57
And using bcmath functions will help:
echo bcmul('0.58', '100', 2);//58.00
php manual:
//Multiply the left_operand by the right_operand.
string bcmul(string $left_operand , string $right_operand [, int $scale = 0 ])
But why will this work?
I noticed that the first two parameters should be string, I wonder if this is because these functions deal with numbers in string way?
Upvotes: 0
Views: 424
Reputation: 8865
The numbers used with bcmath
should be represented as strings because if you use native php number formats like integer or float then you simply can not express all values.
Simplest example ist type integer. Integers are limited to a maximum value as defined by the constant PHP_INT_MAX
. If you would try to write down any number bigger than that php would not be able to represent that exact number. This is due to the fact that php uses a pre defined amount of memory to store integers.
See this example:
echo PHP_INT_MAX;
echo "\n";
echo PHP_INT_MAX + 1;
Output is:
9223372036854775807
9.2233720368548E+18
As you can see the second value is not 9223372036854775808 which would be correct but some other (similar) value.
Upvotes: 1