Click here to Skip to main content
13,558,160 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 19 Jun 2006
Licenced CPOL

Round a number to a multiple of another number

, 19 Jun 2006
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 7:42
memberBarbara Crane25-Mar-13 7:42 
GeneralRe: Minor, but significant change to algorithm Pin
Johan Hakkesteegt25-Mar-13 21:54
memberJohan Hakkesteegt25-Mar-13 21:54 
GeneralWorks like a charm Pin
pimb21-Sep-09 1:36
memberpimb21-Sep-09 1:36 
GeneralI'm glad i found this Pin
frappant30-Jun-09 4:01
memberfrappant30-Jun-09 4:01 
GeneralRe: I'm glad i found this Pin
Johan Hakkesteegt30-Jun-09 19:57
memberJohan Hakkesteegt30-Jun-09 19:57 
GeneralThx! and this is called Quantization... Pin
neurostatic5-Dec-06 15:43
memberneurostatic5-Dec-06 15:43 
GeneralThanks, it's just what I needed Pin
BruceN23-Aug-06 17:27
memberBruceN23-Aug-06 17:27 
GeneralCool :-) Pin
Corinna John19-Jun-06 23:24
memberCorinna John19-Jun-06 23: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.

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