Click here to Skip to main content
15,886,547 members
Articles / Programming Languages / Visual Basic

Round a number to a multiple of another number

Rate me:
Please Sign up or sign in to vote.
3.14/5 (14 votes)
19 Jun 2006CPOL1 min read 48.3K   13   8
Code to round a number to the nearest multiple of another number.

Introduction

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:

VB
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.

License

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


Written By
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)

Comments and Discussions

 
SuggestionMinor, but significant change to algorithm Pin
Barbara C25-Mar-13 7:42
Barbara C25-Mar-13 7:42 
GeneralRe: Minor, but significant change to algorithm Pin
Johan Hakkesteegt25-Mar-13 21:54
Johan Hakkesteegt25-Mar-13 21:54 
Hi Barbara,

Thanks for this example.

I mentioned that the method could be adapted, but never (needed to) got around to writing an example myself.

Best regards,
Johan
My advice is free, and you may get what you paid for.

GeneralWorks like a charm Pin
pimb21-Sep-09 1:36
pimb21-Sep-09 1:36 
GeneralI'm glad i found this Pin
frappant30-Jun-09 4:01
frappant30-Jun-09 4:01 
GeneralRe: I'm glad i found this Pin
Johan Hakkesteegt30-Jun-09 19:57
Johan Hakkesteegt30-Jun-09 19:57 
GeneralThx! and this is called Quantization... Pin
neurostatic5-Dec-06 15:43
neurostatic5-Dec-06 15:43 
GeneralThanks, it's just what I needed Pin
BruceN23-Aug-06 17:27
BruceN23-Aug-06 17:27 
GeneralCool :-) Pin
Corinna John19-Jun-06 23:24
Corinna 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.