ACC2000: How to Round a Number Up or Down by a Desired Increment

This article was previously published under Q209996
This article has been archived. It is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

This article shows you how to create a procedure that rounds a numberup or down by a specified increment. For example, given the number 3.23,rounding to the nearest .05 results in the number 3.25. The procedure inthis article accepts any positive rounding increment as a parameter. Inaddition to rounding numbers to the nearest fractional amount, you canalso round to whole numbers, such as 1, 10, or 100.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following example creates a user-defined function called RoundToNearest, which accepts three parameters:
dblNumberThe numeric value you want to round
varRoundAmountThe increment to which dblNumber will be rounded
varUpAn optional argument: include to round up; omit to round down
For example:
  • RoundToNearest(3.33, 0.1, up) returns the value 3.4.
  • RoundToNearest(3.33, 0.1) returns the value 3.3.
  1. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit
  2. Create the following procedure:
    Function RoundToNearest(dblNumber As Double, varRoundAmount As Double, _   Optional varUp As Variant) As Double               Dim dblTemp As Double   Dim lngTemp As Long         dblTemp = dblNumber / varRoundAmount   lngTemp = Clng(dblTemp)         If lngTemp = dblTemp Then         RoundToNearest = dblNumber   Else      If IsMissing(varUp) Then         ' round down         dblTemp = lngTemp      Else         ' round up         dblTemp = lngTemp + 1      End If      RoundToNearest = dblTemp * varRoundAmount   End IfEnd Function
  3. To test this function, type each of the following lines in the Immediate window, and then press ENTER:
    ?RoundToNearest(1.36, 0.25, up)
    Note that the procedure returns 1.5.
    ?RoundToNearest(1.36, 0.25)
    Note that the procedure returns 1.25.
    ?RoundToNearest(1.36, 0.75, up)
    Note that the procedure returns 1.5, which is two increments of 0.75.
For additional information about rounding, click the article numbers below to view the articles in the Microsoft Knowledge Base:
210564 ACC2000: Round or Truncate Currency Values to the Intended Number of Decimals
210423 ACC2000: Rounding Errors When You Use Floating-Point Numbers
factor cents dollars quarter dime nickel roundup rounddown round-up round-down

Article ID: 209996 - Last Review: 12/05/2015 11:16:56 - Revision: 2.2

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbinfo kbprogramming KB209996