I made the following attempt
Public Function RoundToNearestMultiple(ByVal value As Object, ByVal multiple As Object) As Object
Return (value \ multiple) * multiple
End Function
When I Call the Function
as given below, I get the Result as Shown Against Each Call
MsgBox(RoundToNearestMultiple(1936, 1))
'==>1936
MsgBox(RoundToNearestMultiple(1936, 5))
'==>1935
MsgBox(RoundToNearestMultiple(1936, 10))
'==>1930
MsgBox(RoundToNearestMultiple(1936, 100))
' ==>1900
MsgBox(RoundToNearestMultiple(1936, 500))
' ==>1500
MsgBox(RoundToNearestMultiple(1936, 1000))
' ==>1000
Apparently, the Output is not correct.
What is correct way to do the RoundingOf
to the Nearest Given Multiple
So I tried the following but am still getting incorrect values
Public Function RoundToNearestMultiple(ByVal value As Object, ByVal multiple As Object) As Object
Dim TempMultiple = 1 ^ (Val(Len(Str(multiple))) - 1)
Return Math.Ceiling((value \ multiple) * TempMultiple)
End Function
Seems, I need to use both Math.Floor
and Math.Ceiling
with a If...Else
clause.