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

Complex Rounding Function 1

Status
Not open for further replies.

capone67

Programmer
Nov 6, 2000
115
CA
Hi Gang

I feel really stupid asking this one but it just isn't coming to me. I need to round up a number to the nearest 15. In other words I need 14.2 to round up to 15, I need 15.0 to round to 15, I need 35.000 to round to 45, I also need 29.8 to round to 30.

I tried using the "\" operator but it rounds numbers before it does the operation. This doesn't work properly for me. Anyone have any ideas?

TIA

Ken
 
To round to the nearest 15, use the function:

Public Function Round15(ByVal vNumber As Variant) As Long
Dim lNumber As Long
If Not IsNumeric(vNumber) Then
Round15 = 0
Exit Function
End If
lNumber = 15 * (Int(vNumber / 15))
If vNumber - lNumber >= 7.5 Then
lNumber = lNumber + 15
End If
Round15 = lNumber
End Function

To round to the nearest higher 15, use the function:

Public Function RoundUp15(ByVal vNumber As Variant) As Long
Dim lNumber As Long
If Not IsNumeric(vNumber) Then
RoundUp15 = 0
Exit Function
End If
lNumber = 15 * (Int(vNumber / 15))
If vNumber - lNumber > 0 Then
lNumber = lNumber + 15
End If
RoundUp15 = lNumber
End Function

The reason why I give two examples, is that you said that 35 should round to 45. To the nearest 15, 35 rounds to 30. But if you always want to round up, you can use the second function.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top