TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

Language: Visual Basic
Expertise: Beginner
Jun 4, 1999

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Rounding function

Question:
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 If
End 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.

DevX Pro

 Submit a Tip Browse "XML" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date