Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) [Solved] My totals are off by a penny (Read 1942 times)
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
[Solved] My totals are off by a penny
Oct 1st, 2007 at 6:15pm
Print Post Print Post  
I've run my month-end invoice reports.  Each subtotal line is correct, but the grand total at the bottom of each column is off by a penny, one cent short of the correct total.  Help?
« Last Edit: Oct 1st, 2007 at 9:37pm by Hammer »  

**
Captain Infinity
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: My totals are off by a penny
Reply #1 - Oct 1st, 2007 at 6:17pm
Print Post Print Post  
Are you using Money or Double/Number? Are you rounding or doing math on the various numbers?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: My totals are off by a penny
Reply #2 - Oct 1st, 2007 at 6:58pm
Print Post Print Post  
Quote:
Are you using Money or Double/Number?   Are you rounding or doing math on the various numbers?

Everything is money.   I do lots of addition to get the subtotal on an invoice.  Then, if a customer has a discount percentage, the following is done:
Code
Select All
// Calculates the discount amount

Subroutine CalculateCustomerDiscount()
var vPosDiscAmt as Double
var vDiscountUnrounded as Double
IF Details_Customer_Discount_Percentage > 0
THEN
	{
	vPosDiscAmt = (@ToNumber(Subtotal) * (Details_Customer_Discount_Percentage/100))
	vDiscountUnrounded = (0 - vPosDiscAmt)
	Details_Discount_Amount = (@ToNumber(@Round(vDiscountUnrounded, 2)))
	// if @error then WriteLn("Error calculating discount amount")
	}
ELSE
IF (@ISBLANK(Details_Customer_Discount_Percentage) OR Details_Customer_Discount_Percentage = 0)
THEN
	{
	CLEAR(Details_Discount_Amount)
	IF (@ISBLANK(Details_Discount_Amount) AND Details_Customer_Discount_label = "CUSTOMER DISCOUNT")
	THEN
		{
		CLEAR(Details_Customer_Discount_label)
		}
	}
End Subroutine  


This is the only place rounding is done, and it seems to work well, but this must be where the problem lies.  The Invoice Total is not rounded; should it be?  What's odd is that all the subtotals are spot on, but it's the grand total that's off.

In looking at it I see that I've forced Details_Discount_Amount to "number" with ToNumber.  Should I whack it with ToMoney once it's been calculated?
  

**
Captain Infinity
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: My totals are off by a penny
Reply #3 - Oct 1st, 2007 at 7:42pm
Print Post Print Post  
OK, I changed my subtotals and total to Number rather than Money, and I see figures out to three decimal places on some of them.  So my question now becomes, once the number has been rounded, how can I cement it in place to 2 digits?
  

**
Captain Infinity
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: My totals are off by a penny
Reply #4 - Oct 1st, 2007 at 7:45pm
Print Post Print Post  
Infinity wrote on Oct 1st, 2007 at 7:42pm:
OK, I changed my subtotals and total to Number rather than Money, and I see figures out to three decimal places on some of them.  So my question now becomes, once the number has been rounded, how can I cement it in place to 2 digits?


First thing you'll want to do is change them back to Money. Second thing you'll want to do is wait about a minute as Ben is currently typing up an answer that will explain why the grand total appears to be off by a penny, when in fact it's the right answer.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: My totals are off by a penny
Reply #5 - Oct 1st, 2007 at 8:02pm
Print Post Print Post  
Infinity,
The programming below should work for you. If you are working with money, use the money type.

Code
Select All
// Calculates the discount amount

Subroutine CalculateCustomerDiscount()
var vPosDiscAmt as Money
var vDiscountUnrounded as Money
IF Details_Customer_Discount_Percentage > 0
THEN
	{
	vPosDiscAmt = (@ToMoney(Subtotal) * (Details_Customer_Discount_Percentage/100))
	vDiscountUnrounded = (@ToMoney(0) - vPosDiscAmt)
	Details_Discount_Amount = @Round(vDiscountUnrounded, 2)
	// if @error then WriteLn("Error calculating discount amount")
	}
ELSE
IF (@ISBLANK(Details_Customer_Discount_Percentage) OR Details_Customer_Discount_Percentage = 0)
THEN
	{
	CLEAR(Details_Discount_Amount)
	IF (@ISBLANK(Details_Discount_Amount) AND Details_Customer_Discount_label = "CUSTOMER DISCOUNT")
	THEN
		{
		CLEAR(Details_Customer_Discount_label)
		}
	}
End Subroutine   



Quote:
The Invoice Total is not rounded; should it be?  What's odd is that all the subtotals are spot on, but it's the grand total that's off.


You are going to need to round the Invoice Total before it gets to the report. Your Grand Total in the report is adding up the raw non-rounded values. For example the Grand Total may get 2.444 and 2.642 added to it but on the report you only see 2.44 and 2.64 so your total would be displayed as 5.09 (which is 5.086 rounded) instead of the 5.08 you would expect from adding up the displayed values.
  
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: My totals are off by a penny
Reply #6 - Oct 1st, 2007 at 8:05pm
Print Post Print Post  
Thanks Ben, I'll give it a try.  You guys ROCK!

Should I run this as a Mass Update on the values already saved in the database?

Code
Select All
Details_Discount_Amount = @Round(vDiscountUnrounded, 2)) 


There's a wayward right paren at the end there, BTW.  Smiley
  

**
Captain Infinity
Back to top
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: My totals are off by a penny
Reply #7 - Oct 1st, 2007 at 8:29pm
Print Post Print Post  
Infinity wrote on Oct 1st, 2007 at 8:05pm:
Should I run this as a Mass Update on the values already saved in the database?

No, you will want to run a Mass Update to round the Invoice Total and possibly the Subtotal element, if you want to use that in the report as well.

Note:
I made a change to the programming above that formats 0 as money. The change is highlighted in green below.
vDiscountUnrounded = (@ToMoney(0) - vPosDiscAmt)
  
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: My totals are off by a penny
Reply #8 - Oct 1st, 2007 at 8:32pm
Print Post Print Post  
Quote:
No, you will want to run a Mass Update to round the Invoice Total and possibly the Subtotal element, if you want to use that in the report as well.

I'm not using Subtotal anywhere that I can think of.  So, is all I have to do Mass Update
Code
Select All
Invoice_Total = @ROUND(Invoice_Total, 2) 


or have I missed something?  That seems too easy.  Wink
  

**
Captain Infinity
Back to top
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: My totals are off by a penny
Reply #9 - Oct 1st, 2007 at 8:38pm
Print Post Print Post  
That is all you need to Mass Update.
  
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: My totals are off by a penny
Reply #10 - Oct 1st, 2007 at 8:58pm
Print Post Print Post  
Done.  YAY!  I got my penny back!  I am so rich now!  Thanks Ben.
  

**
Captain Infinity
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: My totals are off by a penny
Reply #11 - Oct 1st, 2007 at 9:34pm
Print Post Print Post  
Be careful not to spend it all in one place Scott.  Wink

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: [Solved] My totals are off by a penny
Reply #12 - Oct 2nd, 2007 at 10:18am
Print Post Print Post  
Ben, the code you gave me is doing funky stuff.  On an invoice with a subtotal of $100.00 and a discount of 5%, the discount amount becomes $10,307,921.51, for an invoice total of $10,308,021.51.

While my boss would like to see such an invoice going out I don't think the customer would be too happy.

I made the following changes and the numbers on the screen appear correct:
Code
Select All
// Calculates the discount amount

Subroutine CalculateCustomerDiscount()
var vPosDiscAmt as Double
var vDiscountUnrounded as Money
IF Details_Customer_Discount_Percentage > 0
THEN
	{
	vPosDiscAmt = (@ToNumber(Subtotal) * (Details_Customer_Discount_Percentage/100))
	vDiscountUnrounded = (0 - vPosDiscAmt)
	Details_Discount_Amount = @Round(vDiscountUnrounded, 2)
	// if @error then WriteLn("Error calculating discount amount")
	}
ELSE
IF (@ISBLANK(Details_Customer_Discount_Percentage) OR Details_Customer_Discount_Percentage = 0)
THEN
	{
	CLEAR(Details_Discount_Amount)
	IF (@ISBLANK(Details_Discount_Amount) AND Details_Customer_Discount_label = "CUSTOMER DISCOUNT")
	THEN
		{
		CLEAR(Details_Customer_Discount_label)
		}
	}
End Subroutine 



I also put a statement elsewhere in my programming to round my grand total every time it is calculated.  I'll need to run some reports to see if this is working.  It's early in the day yet so I'll be experimenting for a while before you see this, but when you do, if there's something else I should be doing please let me know.  Thanks for your help.
  

**
Captain Infinity
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: [Solved] My totals are off by a penny
Reply #13 - Oct 2nd, 2007 at 1:38pm
Print Post Print Post  
Hello Scott,

I just tried the code that Ben gave you and it gives me a discount amount of -5.0000. Could you send me a sample DB and DAT file that shows me what you are seeing? I don't need your entire app just a simple app with a couple of fields that shows the problem will do.

When you are working with Money you are not going to want to be switching it to doubles and then back to Money again. By doing so you are losing some the accuracy that is provided by the new money type.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: [Solved] My totals are off by a penny
Reply #14 - Oct 2nd, 2007 at 4:03pm
Print Post Print Post  
Ben has changed the code that was posted yesterday.  I'll give that a try and report back.  Thanks for your help guys!

UPDATE: The modified code works perfectly.  WOO HOO!  You guys are the best.  THANKS!
  

**
Captain Infinity
Back to top
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print