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

Rounding Up to Nearest 50 cents 3

Status
Not open for further replies.

kennedymr2

Programmer
May 23, 2001
594
AU
Is there a simple way of rounding UP to the nearest .50 cents

eg
12.00 = 12.00
12.01 = 12.50
12.49 = 12.50

12.50 = 12.50
12.51 = 13.00
12.78 = 13.00

I can do it by writing a lot of ifs' etc.
Was interested if there is a simple way of doing this.

Appreciate any help
 
Double it, Minus it, Int it, Half it and minus it
Code:
myOut = -(Int(-myIn * 2)) / 2

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
johnwm,
How simple!!!!, sure beats all the if's etc i was doing.
Have had a pretty good look around the internet for a simple solution, and all the others were complicated.
Much appreciated.

regards kennedymr2
 
Very neat, john. It didn't occur to me that negative numbers will give a kind of "reverse int" process.
 
Thanks guys. Useful trick, but obviously only works for positive arguments, as Int takes you downwards always (more negative), whereas Fix always takes you nearer to zero. You can obviously modify the 'Half and double routine' to round to any integral submultiple

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
<only works for positive arguments
Well, that depends on how you define up, doesn't it? For example, the output of your function on the number -34.58 is -34.5, which is the next higher 50 cents.

Bob
 
You're right of course Bob. I was pointing out that the function was asymmetric, so processing credits and debits would not match.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top