ACC2000: How to Round a Number Up or Down by a Desired Increment
ID: Q209996
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).
SUMMARY
This article shows you how to create a procedure that rounds a number
up 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 in
this article accepts any positive rounding increment as a parameter. In
addition to rounding numbers to the nearest fractional amount, you can
also round to whole numbers, such as 1, 10, or 100.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals 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 needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
MORE INFORMATION
The following example creates a user-defined function called RoundToNearest, which accepts three parameters:
Parameter |
Value |
dblNumber |
The numeric value you want to round |
varRoundAmount |
The increment to which dblNumber will be rounded |
varUp |
An 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.
- Create a module and type the following line in the Declarations section if it is not already there:
Option Explicit
- Create the following procedure:
Function RoundToNearest(dblNumber As Double, varRoundAmount As Double, _
Optional varUp As Variant) As Double
Dim dblTemp As Double
Dim intTemp As Integer
dblTemp = dblNumber / varRoundAmount
intTemp = Int(dblTemp)
If intTemp = dblTemp Then
RoundToNearest = dblNumber
Else
If IsMissing(varUp) Then
' round down
dblTemp = intTemp
Else
' round up
dblTemp = intTemp + 1
End If
RoundToNearest = dblTemp * varRoundAmount
End If
End Function
- 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.
REFERENCES
Additional query words:
factor cents dollars quarter dime nickel
Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbinfo
|