Rounding function

Rounding function

Is there a way to round numbers to the nearest multiple of n, where n could be any number? For example, if n = 5, it would round 8 to 10, 13 to 10, 24 to 25, etc.

There is no native function that I know about that would provide this functionality, but we could create a simple function:

Private Function RoundEx(iValue As Integer, iMod As Integer) As Integer    If iValue Mod iMod >= iMod / 2 Then        RoundEx = iValue + iMod - (iValue Mod iMod)    Else        RoundEx = iValue - (iValue Mod iMod)    End IfEnd Function

Basically, we determine which way we are going to round, up or down. This is done by looking at remainder of the iValue divided by iMod. If it is greater then or equal to iMod/2 then we round up. Otherwise we round down.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist