. Updated Daily. Editions SDA India   SDA Indonesia
BUSINESS ENTERPRISE SOLUTIONS ARCHITECTURE INFORMATION SECURITY WIRELESS & MOBILITY DATA & STORAGE DEVELOPMENT HARDWARE













News

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

 
 
print save email comment

print

save

email

comment

 
 

Search SDA Asia

Free eNewsletter

SDA Asia Magazine Free Download
 
 
 
Copyright @ 2009 SDA Asia Magazine - All Right Reserved Privacy Policy | Terms of Use