Now I store balance
field in MySQL like decimial(15, 2)
. And in PHP I use the bcmath library for operations:
bccomp($currentBalance, 100.15, 2)
- for check if user have enough money.
bcadd($currentBalance, 100)
- for increase user balance.
And etc.
This is working correctly, but BCMath doc says:
Passing values of type float to a BCMath function which expects a string as operand may not have the desired effect due to the way PHP converts float values to string, namely that the string may be in exponential notation (what is not supported by BCMath), and that the decimal separator is locale dependend (while BCMath always expects a decimal point).
So I want to avoid surprises. What is the right way to deal with money?