Monday, 2 April 2007
Money Calculations in PHP |
| |
|
| |
Stas in a post over his blog gives you techniques to do money calculations in PHP. He says since PHP has no BCD type and no arbitrary precision float type either, its no use using regular floats/doubles in this case. He informs that there’s even no precise representation for number as simple as 0.1. He warns that if you are making a lot of calculations with such numbers errors may creep in.
He gives a solution to it. He tells you to make all calculations in units like one hundredth or thousandth of a cent and make everything integer, using arbitrary-precision integers like in GMP and then when printing it just shift the point and cut the extra digit whenever needed.
Another solution he says is to have a type that combines arbitrary-precision integer and decimal point position, like Java does.
|
| |
|
Read the Post
|
| |
|
|
| |
|
|
| |
|