ScubaStevo
Programmer
How can I round a number to the nearest 5 or 10? So far I can only manage to the nearest whole number (or any number of of decimal places) with the Round function.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Function basRound(Number As Double, Num_Digits As Integer)
'If Num_Digits < 0, this "subtracts" the decade's values
'If Num_Digits = 0, this returns the Integer Portion
'If Num_Digits > 0, this returns the value "rounded" to the number of
'Decimal palaces of Num_Digits.
Dim dblNumNum_Digits As Double 'Set up variables
Dim intNumNum_Digits As Long
'Move decimal point such that int will perform at correct level
dblNumNum_Digits = Number * (10 ^ Num_Digits)
intNumNum_Digits = CLng(dblNumNum_Digits) 'Integerise
'Compare integer to see if needs rounding up
intNumNum_Digits = Round(dblNumNum_Digits, 0) 'Round
'Return result, moving decimal point back
basRound = intNumNum_Digits / (10 ^ Num_Digits)
End Function\
Public Function BasRound2Val(Number As Double, Multiple As Single) As Double
'Michael Red. 2/16/2002. To Round UP to the Multiple
'Usage: ? BasRound2Val(1.001, 0.25)
' 1.25
'Set up variables
Dim dblDivided As Double
Dim intDivided As Integer
dblDivided = Number / Multiple ''Divide
intDivided = Int(dblDivided) 'Integerise
intDivided = Round(dblDivided, 0) 'Round
'Return result, returning to nearest multiple
If (intDivided = dblDivided) Then
BasRound2Val = (intDivided * Multiple)
Else
BasRound2Val = (intDivided * Multiple) + Multiple
End If
End Function
'** Modified 20050830 by PIB **
...
If Number > 15 And Multiple = 5 Then
If Val(Right(Str(Int(Number)), 1)) <= 5 Then
BasRound2Val = Number + (Multiple - (Number - (Int(Number / 10) * 10)))
Else
BasRound2Val = Number + (Multiple - (Number - (Int(Number / 10) * 10))) + 5
End If
Else
'** end of Modified 20050830 by PIB **
'Return result, returning to nearest multiple
If (intDivided = dblDivided) Then
BasRound2Val = (intDivided * Multiple)
Else
BasRound2Val = (intDivided * Multiple) + Multiple
End If
End If
End Function
Phil said:" ... a single command ... "