RE: [PHP] Math Weirdness - Can it be done with integers alone??

From: Date: Mon, 14 Jul 2008 20:09:04 +0000
Subject: RE: [PHP] Math Weirdness - Can it be done with integers alone??
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-276787@lists.php.net to get a copy of this message
[quote] I don't quite understand your problem, but I use integers for any monetary workings as you can guarantee it is accurate (obviously, you work in pence or cents rather than GBP or USD). Alex Hello Alex, I was reading through this thread, and I was curious about what methods you use to handle fractions of a dollar and/or fractions of a penny if you are always using integers.  Do you only use a decimal for printing?  do you adjust all interest rates, etc...then as well?  How about when interest calculations result in fractions of pennies, how do you handle it then?  Basically, I can't see how it could be done with just integers alone?  thanks dank [/quote] To be honest, I haven't used it for any complicated use - shopping carts are additive contraptions. However, I don't see the problem. 1) I use Smarty a lot - I use a plugin to format the integer to a price (ie divide by 100 and display in 2 decimal places). So yes, I only put the decimal place there to print. 2) Why do interest rates need to be changed?? Take £123.45 @ 5%. 123.45 * 1.05 = (12345 [pennies] / 100) [pounds] * 1.05 = (12345 * 1.05) / 100 Same calculation in my mind!! (Tell me if I have missed the point!!) 3) If you have a fraction of a penny - round it!! Surely, that's what the banks do!! It's a penny!! I think it can be done with integers alone - let me know if you disagree!! Alex No virus found in this outgoing message. Scanned by AVG Free 8.0 Checked by AVG - http://www.avg.com Version: 8.0.138 / Virus Database: 270.4.10/1550 - Release Date: 13/07/2008 17:58

« previous php.general (#276787) next »