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

Round to nearest .5 1

Status
Not open for further replies.

MisterC

IS-IT--Management
Apr 19, 2001
501
US
What is the easiest way to round to the nearest .5? --------------
A little knowledge is a dangerous thing.
 
Hi,
Maybe you can adapt this to your needs.
There must be a more elegant way, but I
couldn't find one.
Place two text boxes and a button on the form
to try it.
Copy and paste this code into your code module.

HTH
Rowland

Private Sub Command1_Click()
Dim IntPart As Integer
Dim DecPart As Double
Dim RoundedNum As String
Dim RndPart As String
Dim AddOne As Integer

IntPart = Int(CDbl(Text1.Text))
DecPart = CDbl(Text1.Text) - Int(CDbl(Text1.Text))

Select Case DecPart
Case Is < 0.25
RndPart = &quot;.0&quot;
Case 0.25 To 0.75
RndPart = &quot;.5&quot;
Case Is > 0.75
AddOne = 1
RndPart = &quot;.0&quot;
End Select

RoundedNum = CStr(IntPart + AddOne) & RndPart
Text2.Text = RoundedNum

End Sub
 
Another way..

k = IIf(Text1.Text - Int(Text1.Text) > 0.5, _
Int(Text1.Text) + 0.5, Int(Text1.Text))

Nearestvalue = IIf(Text1.Text - k > 0.25, k + 0.5, k)

It has to be improvised for values less than zero.

-Sekhar
 
Or:

[tt]
Private Function RoundIt(dValue As Double) As Double
RoundIt = (0.5 * CInt(dValue / 0.5))
End Function
 
Thanks for the help! --------------
A little knowledge is a dangerous thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top