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

Round numbers to the nearest 0.5

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Hi Folks,

I need to be able to round a number to the nearest 0.5.

i.e. 21.3 rounds to 21.5
21.25 rounds to 21

Any suggestions on how to do this.

Just wondering if something like

Code:
Round(Round(2*myVal, 0)/2, 1)

would work consistently or if there is a better method

Nick

Mighty
 
Actually,

that fornula won't work for 21.25 as it will round it to 21.5 which is not correct.

Any other suggestions.

Mighty
 
Since this is custom rounding, based on your own rules, I would write a quick function that ensures the right rounding:
Code:
Function myRounded(myVal)
   myVal = FormatNumber(myVal,2)
   myVal = Split(myVal,".")
   ' myVal(0) is the whole number before the decimal
   ' myVal(1) is the decimal value, two places
   myDec = cint(myVal(1))
   if myDec>25 and myDec<76 then
      myRounded = cDbl(myVal(0)+.5)
   else
      if myDec>75 then
         myRounded = cInt(myVal(0)+1)
      else
         myRounded = cInt(myVal(0))
      end if
   end if
End Function
Perhaps there's an easier way to do it, but that's how I would tackle a problem like this. I tested this function on my machine with a couple examples to ensure it follows your rules. It bascially looks at the decimal of the passed value as a two-digit number, then if it's between 26 and 75 it returns the original number with a .5, greater than 75 returns the original number +1, if it's 25 or less it will return the original number.

Eric
 
Wow, deja-vu. I swear someone asked this question in the past few years...good luck trying to find it though :p

I believe the double rounding will work consistently. Of course since I skipped over part of your question and already had a solution before I re-read your question and realized we worked it out similarly.

My multi-use function:
Code:
Function NearestRound(OrigNumber,nearDec)
   Dim factor: factor = 1/nearDec
   Dim baseNum, dec
   baseNum = Fix(OrigNumber)
   dec = Round((OrigNumber - baseNum)*factor,0)
   NearestRound = baseNum + dec/factor
End Function

Or a shorter version:
Function NearestRound(OrigNumber,nearDec)
   NearestRound = Fix(OrigNumber) + Round((OrigNumber - Fix(OrigNumber))/nearDec,0) * nearDec
End Function

OrigNumber is the number you wish to round, nearDec is the nearest decimal you wish to round to. So in this instance you would do something like:
Response.Write NearestRound(yourNumVar,.5)


I think the biggest difference between our methods is that rather than use a double round I ended up using the Fix function. Round() is not always as consistent as you would think it would be. I believe it uses Bankers rounding, but I could be mis-remembering. So I used a Fix() statement to take the non-decimal portion out of the equation.

Looking at it more, I'm almost thinking that you could use a single round and consistently get the correct number:
Code:
Function NearestRound(OrigNumber,nearDec)
   NearestRound = Round(OrigNumber/nearDec,0) * nearDec
End Function

The only concern I would have is large floats that might cause vbscript to add/subtract .000000001 to them when you do the end multiplication.

-T


 
Thanks for all the suggestions guys

Mighty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top