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

Round number down to nearest .5

Status
Not open for further replies.

thegentleman

IS-IT--Management
Joined
Apr 4, 2001
Messages
65
Location
GB
Hi,

I need to round a decimal point number DOWN to the nearest .5 i.e.

3 > 3
3.1 > 3
3.6 > 3.5
3.9 > 3.5
4 > 4

Any help would be appreciated!

~tg
 
I wrote this function to handle it.....

Code:
	Function RoundNearestHalf(iTot) 
		dim sNum, sDigit, iNum
		sNum = Round(iTot / 12,1)

		sDigit = iif( inStr(1, sNum, ".") > 0 , CStr(Right(sNum,1)), "0")
		iNum = Int(sNum)
		
		if sDigit >= &quot;1&quot; and sDigit <= &quot;5&quot; then
			sDigit = &quot;5&quot;
		elseif sDigit >= &quot;6&quot; and sDigit <= &quot;9&quot; then
			sDigit = &quot;0&quot;
			iNum = iNum + 1
		end if 
		
			
		RoundNearestHalf = iNum & &quot;.&quot; & sDigit
	End Function
 
Spot on! :-)

Cheers mate, you've saved me a headache!

~tg
 
oops... pasted wrong version.

ex RoundNearestHalf(2.3)

Code:
    Function RoundNearestHalf(sNum) 
        dim sDigit, iNum

        sDigit = iif( inStr(1, sNum, &quot;.&quot;) > 0 , CStr(Right(sNum,1)), &quot;0&quot;)
        iNum = Int(sNum)
        
        if sDigit >= &quot;1&quot; and sDigit <= &quot;5&quot; then
            sDigit = &quot;5&quot;
        elseif sDigit >= &quot;6&quot; and sDigit <= &quot;9&quot; then
            sDigit = &quot;0&quot;
            iNum = iNum + 1
        end if 
        
            
        RoundNearestHalf = iNum & &quot;.&quot; & sDigit
    End Function
 
Actually, that function rounds up not down. That's not a problem as it gives me a basis to work from which is all I needed.

Thanks again!
 
Actually, that function rounds up not down. That's not a problem as it gives me a basis to work from which is all I needed.

Also, what happens if the inputted number does not have a decimal place, i.e. 10?

Thanks again!
 
I was playing a bit and came up with this. I know theres a mathmatical expression but just getting the bugs out of it.

Function Deci(MyNum)

Dim Arg : Arg = Split(MyNum,&quot;.&quot;)

If UBound(Arg) > 0 Then
If Arg(1) > 5 Then
Arg(1) = .50
MyNum = Arg(0)+Arg(1)
Deci = cDbl(MyNum)

ElseIf Arg(1) < 5 Then
Arg(1) = .00
MyNum = Arg(0)+Arg(1)
Deci = cDbl(MyNum)
End If
Else
Deci = cDbl(MyNum)
End If

End Function

____________________________________________________
Python????? The other programming language you never thought of!
thread333-584700

onpnt2.gif
 
The line where you set the value of sDigit handles it.


ps.

since vbscript is lacking an iif I made one.

function iif(i_Expr, i_True, i_False)
if i_Expr then
iif = i_True
else
iif = i_False
end if
end function
 
Thanks again guys. You've given me plenty to work with.
 
I know this is very after-the-fact, but I figured I'd share anyway. Try this:

CLng([YourNumber] * 2 - 0.5)/2
 
Hello all,

Dependent on what wanted on negative number, round &quot;down&quot; can be done in one line
Code:
     int(x*2) / 2
or   fix(x*2) / 2
or this is the normal round
     round(x*n) / n
In fact, round to n-partition of unity can be done similarly
Code:
     int(x*n) / n (etc)
regards - tsuji
 
Check out this thread from the vbscript forum:
thread329-620727

Particularly recommend the link I posted at the end - a function that forces round-to-larger instead of round-to-even (what the built-in round() function uses).

As for your question, tsuji's looks like a simple solution.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top