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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formula for Rounding up $25 more than a number 2

Status
Not open for further replies.

crewchiefpro6

Programmer
Mar 22, 2005
204
US
I need a formula to round up from a number to the nearest $25.

For example: is the number is 355.67 I want to calculate that the new number should be 375.00. If the number is 376.00 then the new value should be 400.00.

Before I try to reinvent the wheel does anyone have some tips on this.



Don Higgins
 
Not that hard

Code:
Function RoundUp
  Parameters m.MyValue,m.MyRange
  Private m.MyValue,m.MyRange, m.RetVal
  m.RetVal = Int((m.MyValue-1)/m.MyRange)
  m.RetVal = (m.RetVal+1) * m.MyRange
Return(m.RetVal)

You need to put the error checking in there!




Regards

Griff
Keep [Smile]ing
 
Err, I forgot - you use it like this:

Code:
? Roundup(26,25)
? Roundup(206,25)
? Roundup(276,25)
? Roundup(223,25)

and you get:
Code:
50
225
300
225

Regards

Griff
Keep [Smile]ing
 
Ah, does that mean you want 250 rounded up to 275 as well?

Regards

Griff
Keep [Smile]ing
 
Cool

I *thought* the little sample would leave 250, and similar 'exact divisors' alone, but maybe I missed something - good luck




Regards

Griff
Keep [Smile]ing
 
The only changes I had to make in the math is if the amount was 400.01 it reverted back to 400 instead of moving to 425.00. Minor really, you saved me a bunch of time already. Thanks.


Don Higgins
 
Ah, I was only thinking in integers! Of course your example had pennies in it (cents to you?).

Good luck, always fun doing stuff like that!

Martin

Regards

Griff
Keep [Smile]ing
 
The ceiling function is a bit more efficient...

m.RetVal = CEILING(m.MyValue/m.MyRange)*m.MyRange

Brian
 
Now *that* is clever, every time I answer a question in TT someone points me toward an inbuilt function that I *should* know so much better!

Still, as a caveman, I like to invent the wheel every day!

Ta Brian

Regards

Griff
Keep [Smile]ing
 
Brian, I tried your suggestion but 400.01 still returned 400 instead of 425, am I missing something?

In the end I check to see if the values end on either 25 or 00, and if they do then I use the same value for both.


Don Higgins
 
No, you aren't missing anything. The ceiling fuction rounds up, floor function rounds down. You do need a cents work around, but you never made it clear what your cutoffs are.... what is the 1st value you do want to make 425... 401?

Brian
 
if so...

Code:
m.RetVal = ;
	IIF(m.MyRange*((m.MyValue/m.MyRange)-;
		FLOOR(m.MyValue/m.MyRange))<1,
			FLOOR(m.MyValue/m.MyRange)*m.MyRange,;
			CEILING(x/m.MyRange)*m.MyRange)
 
Very interesting.

For example, if the payment was 400.01 I want the returned value to be 425.00. If the payment was 324.99 the returned value should be 325.00.

I completely forgot about Floor and Ceiling. That is much more elegant than my fix.

Don


Don Higgins
 
If you want to work in pennies, cents, you can do this instead:

Code:
Function RoundUp
  Parameters m.MyValue,m.MyRange
  Private m.MyValue,m.MyRange, m.RetVal
  ** to work with pence (i.e. decimals to two places) multiply numbers by 100
  ** do the math as before
  m.MyValue = m.MyValue * 100
  m.MyRange = m.MyRange * 100
  m.RetVal = Int((m.MyValue-1)/m.MyRange)
  m.RetVal = (m.RetVal+1) * m.MyRange
  ** Then divide the result by 100 again
  m.RetVal = m.RetVal / 100
Return(m.RetVal)

Regards

Griff
Keep [Smile]ing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top