Normal Topic Money rounding, again (Read 517 times)
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Money rounding, again
Jun 7th, 2007 at 8:17pm
Print Post Print Post  
I've searched the forum but have had no luck...was there a fix developed for when Money is not rounded correctly?

I use the following to calculate a customer discount:
Code
Select All
// Calculates the discount amount

Subroutine CalculateDiscount()
var vPosDiscAmt as Double
IF DISCOUNT_PERCENT > 0
THEN
	{
	vPosDiscAmt = BILLING_SUBTOTAL * (DISCOUNT_PERCENT/100)
	DISCOUNT_AMOUNT = 0 - vPosDiscAmt
	if @error then WriteLn("Error calculating discount amount")
	}
ELSE
IF (@ISBLANK(DISCOUNT_PERCENT) OR DISCOUNT_PERCENT = 0)
THEN
	{
	CLEAR(DISCOUNT_AMOUNT)
	}
End Subroutine 


This is in Global Code and is called when the DISCOUNT_PERCENT is changed.  I get some rounding errors.  For instance, if BILLING_SUBTOTAL is $1.50 and DISCOUNT_PERCENT is 5,  DISCOUNT_AMOUNT becomes $-0.07.  The actual value is -0.075, which I would like rounded up to -0.08.  If BILLING_SUBTOTAL is $1.57 and DISCOUNT_PERCENT is 10, DISCOUNT_AMOUNT becomes $-0.15.  The actual value is -0.157, which I would like rounded up to -0.16.  The only values I ever deal with for DISCOUNT_PERCENT are 5, 10 and 100.

Any pointers to some code that could fix this?  Is there a better way to calculate a percentage, perhaps a function I'm not aware of?  Thanks in advance.
  

**
Captain Infinity
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Money rounding, again
Reply #1 - Jun 8th, 2007 at 1:21am
Print Post Print Post  
The following, derived from your code, does what you want. Note the call to @Round in the writeln statement.

Code
Select All
var vPosDiscAmt as Double
var billing as double
var discount as double

	billing = 1.50
	discount = 5
	vPosDiscAmt = billing * (discount / 100)
	discount = 0 - vPosDiscAmt
	writeln(@Round(discount, 2))
 

  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
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: Money rounding, again
Reply #2 - Jul 5th, 2007 at 8:41pm
Print Post Print Post  
Thanks Mark.  Based on this and some advice Ray gave me at the conference (using ToNumber to adjust my subtotal before performing math on it) I have come up with this:
Code
Select All
// Calculates the discount amount

Subroutine CalculateDiscount()
var vPosDiscAmt as Double
var vDiscountUnrounded as Double
IF DISCOUNT_PERCENT > 0
THEN
	{
	vPosDiscAmt = (@ToNumber(BILLING_SUBTOTAL) * (DISCOUNT_PERCENT/100))
	vDiscountUnrounded = (0 - vPosDiscAmt)
	DISCOUNT_AMOUNT = (@Round(vDiscountUnrounded, 2))
	if @error then WriteLn("Error calculating discount amount")
	}
ELSE
IF (@ISBLANK(DISCOUNT_PERCENT) OR DISCOUNT_PERCENT = 0)
THEN
	{
	CLEAR(DISCOUNT_AMOUNT)
	}
End Subroutine 



This seems to be working, but it feels unwieldy, what with two variables getting into the mix.  Is there a more elegant way to do this?
  

**
Captain Infinity
Back to top
IP Logged