Click here to Skip to main content
11,933,621 members (54,469 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked

Round a number to a multiple of another number

, 19 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Code to round a number to the nearest multiple of another number.


While developing an application for our purchase department, I came across a small problem that I could not find any ready made code samples for. Rounding a number up or down is real easy as long as you are talking about number of decimals and/or if you always want to round up or always down. Our purchaser wanted my application to suggest an amount of items to order, rounded up or down to the nearest multiple of the number of items per box.

After writing and abandoning many, many, many lines of code, I finally resorted back to the K.I.S.S. principle and wrote a small function that can easily be inserted anywhere in existing code. I suppose it is not something many people will ever need, but considering how long it took me to figure this one out, I can imagine that I will make at least a few people happy with a ready made solution.

The code

Here is the code, just insert it anywhere in your application, and call it as you see fit. Feed it the number you want rounded and the significance you want it rounded to. The function then returns an integer value that is the closest multiple of the significance to the number:

Public Function RoundToSignificance(ByVal number As Integer, _
       ByVal significance As Integer) As Integer
    'Round number up or down to the nearest multiple of significance'
    Dim d As Double
    d = number / significance
    d = Round(d, 0)
    RoundToSignificance = d * significance
End Function

Final note

The function as is needs two integer values and returns an integer value, but I guess it can easily be adapted to accept and/or return some other type. As a beginner, I can also imagine that there may actually be some existing function or method, but I couldn't find it. If anyone knows of a simpler way to do what this little function does, please let me know.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Johan Hakkesteegt
Software Developer
Finland Finland
My greatest coding accomplishment ever:

MsgBox("Are you sure you wish to erase the content from all your hard drives?", MsgBoxStyle.Critical)

You may also be interested in...

Comments and Discussions

SuggestionMinor, but significant change to algorithm Pin
Barbara Crane25-Mar-13 8:42
memberBarbara Crane25-Mar-13 8:42 
GeneralRe: Minor, but significant change to algorithm Pin
Johan Hakkesteegt25-Mar-13 22:54
memberJohan Hakkesteegt25-Mar-13 22:54 
GeneralWorks like a charm Pin
pimb21-Sep-09 2:36
memberpimb21-Sep-09 2:36 
GeneralI'm glad i found this Pin
frappant30-Jun-09 5:01
memberfrappant30-Jun-09 5:01 
GeneralRe: I'm glad i found this Pin
Johan Hakkesteegt30-Jun-09 20:57
memberJohan Hakkesteegt30-Jun-09 20:57 
GeneralThx! and this is called Quantization... Pin
neurostatic5-Dec-06 16:43
memberneurostatic5-Dec-06 16:43 
GeneralThanks, it's just what I needed Pin
BruceN23-Aug-06 18:27
memberBruceN23-Aug-06 18:27 
GeneralCool :-) Pin
Corinna John20-Jun-06 0:24
memberCorinna John20-Jun-06 0:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by Johan Hakkesteegt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid