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

rounding to the nearest half unit (ex: 47.0 or 47.5)

Status
Not open for further replies.

montypython1

Technical User
Jan 12, 2005
187
US
Greetings,

What is the best way to round to the nearest half unit (ex: 47.0 or 47.5)?

I know how to use the ROUND() function, but that rounds to the nearest whole number.

Any suggestions?

Thanks,
Dave Higgins
 
Take a closer look. It does offer decimal places. Nevertheless that doesn't enable you to round to .5, but you can add that feature easily:

n=47.3
? ROUND(n*2,0)/2

Bye, Olaf.
 
If you are doing 'normal' rounding, Olaf's method works just fine.

If you are doing 'scientific' rounding, that method gives incorrect amounts a small percentage of the time.


mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
That scientific rounding would have no significance whatsoever, as by the definition of rounding to the nearest half unit, a number like N.5 (N being a natural number) would stay as it is, it already is a multiple of .5.

You could apply scientific rounding on the double value (in a case like 47.25, scientific rounding applied to 2*47.25 = 94.5 would round down to 94, yielding 47 instead of 47.5 as the end result). Dave Higgins has to decide whether that should be handled this way. It's not hard to do also, just check bit 0 of Floor(n*2) and if 0, use Floor instead of Round. But I don't think this is asked for.

The resoning just is making the rounding errors point up and down evenly, not having a tendency for adding to values, especially if the numbers you round just have one significant digit, eg if you measure one digit only, as that makes +.5 in 10% of all cases. As soon as you have values with several digits, these special cases don't contribute as much to an overall tendency anyway. Eg rounding prices that way, having 2 digits in most cases, you'd only have a tendency of 1% more than averagely adding instead of subtracting, if your original numbers are having even more digits to them, the tendency effect is neglectible.

Bye, Olaf.
 
Thanks Olaf and Mmerlinn,

That is exactly what I needed ... I should have thought about simply doing the math instead of looking for a different built in function.

As always, I appreciate your suggestions.

Dave Higgins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top