This is a discussion on RE: [PHP] Math Weirdness within the PHP General forums, part of the PHP Programming Forums category; > I am totally buffaloed by a set of very simple calculations that I am > doing; > > /* calculate ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
> I am totally buffaloed by a set of very simple calculations that I am > doing; > > /* calculate total balance with payments and adjustments */ > $totalBalance = $acct['BALANCE'] + $adjBalance; > echo number_format($totalBalance, 2, '.', '')."\t"; > > /* calculate total charges */ > $totalCharges = $intlLDCharges + $longDistance + $smsCharges + > $daCharges + $totalData + $roaming; > echo number_format($totalCharges, 2, '.', '')."\t"; > > /* > * calculate difference between total balance and total charges > * if the amount matches the ending balance then all is OK > * if not calculate the difference > */ > $totBalDiff = $totalBalance - $totalCharges; > if($totBalDiff === $endingBal){ > echo "OK\t"; > } else { > /* what is the difference between the ending balance and the > charges? */ > $totChargeDiff = $endingBal - $totalCharges; > echo number_format($totChargeDiff, 2, '.', '')."\t"; > } > > Each number represented by a variable in all of these calculations has > been rounded to 2 decimal points at the point they are generated. For > the most part this works just hunky-dory but I have a handful of calcs > (out of 300k plus records) that look like this.... > > $endingBal 0.10 > $totalBalance 0.30 > $totalCharges 0.20 > $totalChargeDiff -0.10 > > The balance minus the charges does equal the ending balance as it > should but it is saying that it doesn't and that there a 20 cent swing > (-0.10 is 20 cents different than 0.10). > > I must be missing something. When I echo out raw data I do not see > negative signs. Does anyone have any insight as to what might be > happening here? 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 |
|
|||
|
> > 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:
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 |
|
|||
|
On Mon, 2008-07-14 at 15:00 -0500, Daniel Kolbo wrote:
> > > > 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? Depends on what you're working with. I don't think banks can get away with using pennies. But a storefront can usually do so since they don't charge fractions of pennies and don't offer interest :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP |
|
|||
|
On Mon, 2008-07-14 at 21:09 +0100, Alex Chamberlain wrote:
> 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!! Nope, banks can't round like that when calculating your daily interest :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP |
|
|||
|
On Mon, Jul 14, 2008 at 4:15 PM, Robert Cummings <robert@interjinn.com> wrote:
> > Nope, banks can't round like that when calculating your daily > interest :) If it works in their favor, you'd be surprised what they do. That's why extending TILA to banks in the US is such a big deal to me, yet gets no attention. -- </Daniel P. Brown> Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just $59.99/mo. with no contract! Dedicated servers, VPS, and hosting from $2.50/mo. |
|
|||
|
On Mon, 2008-07-14 at 16:25 -0400, Daniel Brown wrote:
> On Mon, Jul 14, 2008 at 4:15 PM, Robert Cummings <robert@interjinn.com> wrote: > > > > Nope, banks can't round like that when calculating your daily > > interest :) > > If it works in their favor, you'd be surprised what they do. > That's why extending TILA to banks in the US is such a big deal to me, > yet gets no attention. I'm not completely sure about the U.S. but banks in Canada are regulated... fortunately :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP |
|
|||
|
> On Mon, 2008-07-14 at 16:25 -0400, Daniel Brown wrote:
> > On Mon, Jul 14, 2008 at 4:15 PM, Robert Cummings > <robert@interjinn.com> wrote: > > > > > > Nope, banks can't round like that when calculating your daily > > > interest :) > > > > If it works in their favor, you'd be surprised what they do. > > That's why extending TILA to banks in the US is such a big deal to > me, > > yet gets no attention. > > I'm not completely sure about the U.S. but banks in Canada are > regulated... fortunately :) > > Cheers, > Rob. Ok, I may have been wrong about the banks thing - don't quote me on it!! However, there must be some degree of accuracy?? Which can then be reflected in your integers. 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 |
|
|||
|
On Mon, Jul 14, 2008 at 4:28 PM, Robert Cummings <robert@interjinn.com> wrote:
> > I'm not completely sure about the U.S. but banks in Canada are > regulated... fortunately :) They are here, too, to a certain extent. The problem is, they can create their own rules that cost the customer a fortune, and still somehow complain that they need to have rates cut, raised, or otherwise modified so that they can make more of a profit. I have to empathize with them, though. I don't know how I'd survive if I was someone like former Wachovia CEO Ken Thompson, who only got a $7 Million bonus in 2004 (in addition to $11.1 Million in salary, stock, et cetera). And speaking of Wachovia, using them by example, the audacity isn't even masked: "Shares of Wachovia dropped as much as 11 percent to their lowest level since 1991 after the bank said it might lose as much as $2.8 billion in the second quarter. It declined to offer specifics on how it plans to reduce balance sheet risk or whether it needs more capital or might again cut the dividend. "Wachovia said it approached [Robert] Steel for the [position of CEO], offering him a package that could total $38.1 million in salary, bonus and other awards in his first year." I'm no genius (despite what MENSA may say ;-P), but if I was so horribly worried about how to begin cutting expenses, the first thing I'd do is STOP RAISING @$#%ING SALARIES. If I even got a cost of living increase, I'd be happy.... but if I got an exorbitant cost of living increase in the millions, you wouldn't hear me complain. And I'm sure many of you remember when Exxon Mobil CEO Lee Raymond retired in December of 2006. Despite rising gas and oil prices, he "was compensated more than $686 million from 1993 to 2005, according to an analysis done for the New York Times by Brian Foley, an independent compensation consultant. That is $144,573 for each day he spent leading Exxon's "God pod," as the executive suite at the company's headquarters in Irving, Texas, is known. "Despite the company's performance, some shareholders, academics, corporate governance experts and consumer groups were taken aback when they learned for the first time this week the details of Raymond's total compensation package, including the more than $400 million he received in his final year at the company." That's as far OT as I'm going, but it's worth noting how regulation doesn't do shit if it's not enforced. -- </Daniel P. Brown> Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just $59.99/mo. with no contract! Dedicated servers, VPS, and hosting from $2.50/mo. |
|
|||
|
At 4:15 PM -0400 7/14/08, Robert Cummings wrote:
> > >Nope, banks can't round like that when calculating your daily >interest :) > >Cheers, >Rob. I do know that when it comes to interest you pay them, they round up. When it comes to interest they pay you, they round down -- and why not? A decision has to be made -- you can't pay someone less than a penny. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |