Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Solution For Unique Rounding Problem

Status
Not open for further replies.

SGLong

Programmer
Jun 6, 2000
405
US
I need to take some security prices (i.e. Stocks, Bonds, etc.) and round them up to the nearest $0.05. For example, 21.4358 becomes 21.45 and 14.5110 becomes 14.55. Does anyone have any programmatic solutions for this other than a "brute force" CASE construct?

Steve
 
Steve, try this:
Code:
lcPrice = 21.4538
lcNearest = 0.05
lcFrac = MOD(lcPrice,lcNearest)
lcRoundedPrice = lcPrice+IIF(lcFrac>=lcNearest/2,lcNearest,0)-lcFrac)
 
Oops, remove that last ")" on the last line, like this:
Code:
lcPrice = 21.4538
lcNearest = 0.05
lcFrac = MOD(lcPrice,lcNearest)
lcRoundedPrice = lcPrice+IIF(lcFrac>=lcNearest/2,lcNearest,0)-lcFrac
 
Another oops. My prior code rounded to the nearest 0.05 ... this revised code will always round UP to the next highest 0.05:
Code:
lcPrice = 21.4538
lcNearest = 0.05
lcFrac = MOD(lcPrice,lcNearest)
lcRoundedPrice = lcPrice+IIF(lcFrac=0,0,lcNearest-lcFrac)
 
Well 0.05 is 1/20, so you could compute CEILING(amount*20)/20 and have rounded to the next bigger or equal multiple of .05

BYe, Olaf.
 
Thanks for all the tips. I found an associate who was a whiz at Algebra, and with his help this is what we came up with:

Code:
nRtnVal = (nPrice + 0.05) - MOD(nPrice, 0.05)

DbMark was probably the closest to the solution we came up with. Following his lead, changing the '0.05' to a parameter value this could be use to find the next highest anything - dimes, quarters, etc.

Steve


 
Hi Steve,

think about the special case of nPrice being an exact mathc, eg 14.55 should not be rounded to 14.60, shoudl it?

Code:
? (14.55 + 0.05) - MOD(14.55, 0.05)
? Ceiling(14.55*20)/20

In fact DBMark does it right with his IIF(). This does care for this special case, but Ceiling does so, too.

Bye, Olaf.
 
Hi Steve

This is how I round our prices

IIF(MOD(Lcprice,.05)#0,Lcprice-MOD(Lcprice,.05)+.05,Lcprice)

Charlie Huff
CJ's Homecenter












 
Besides, you can also use the Ceiling formula with different precisions:

nPrecision = 0.05 && or 0.10 or 0.25 or whatever
lnRounded = Ceiling(nAmount/nPrecision)*nPrecision

And with nAmounts being an exact multiple of nPrecision this does not result in the next higher multiple amount without any IF or IIF.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top