ACC2000: Rounding Errors When You Use Floating-Point Numbers (210423)
The information in this article applies to:
This article was previously published under Q210423 Novice: Requires knowledge of the user interface on single-user computers.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
SYMPTOMS
When you calculate by using floating-point numbers, the result is not always what you expect.
CAUSE
Errors similar to the example in the "More Information" section of this article occur in any programming language that uses floating-point numbers. This is because decimal fractions do not always have exact binary equivalents, which can result in rounding errors.
RESOLUTION
There are several methods that you can use to avoid rounding errors when you are using floating-point numbers:
- Use the Decimal, Numeric, Currency, Money or SmallMoney data type. These data types are stored as scaled integers instead of floating point numbers, which prevents rounding errors from occurring.
NOTE: The monetary data types (Currency, Money and SmallMoney) are accurate to four decimal places or fewer. The accuracy of the Decimal and Numeric data types depend on the Percision and Scale settings.
- If you are using Single numbers, convert them to Doubles and use formatting to hide the extra digits. If there are any rounding errors, they will occur at the end of the numbers where they will not affect the visible data.
- Place the CDec function around each component of the expression that could possibly contain decimal data. It is not necessary to use CDec around components of an expression that are guaranteed to be whole numbers.
x = CDec(4.555) * 100
- Break the calculation into two or more steps. When you list a calculation in one long line, Visual Basic stores the intermediate values internally. For example, in the calculation
CInt(4.555 * 100)
the value 455.5 is temporarily stored and used in the CInt function. If you break the calculation into the two steps
x = 4.555 * 100
CInt(x)
you avoid this internal storage, and therefore avoid the floating-point rounding error.
REFERENCESFor more information about data types, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Data Types in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
Modification Type: | Major | Last Reviewed: | 6/24/2004 |
---|
Keywords: | kbprb kbprogramming KB210423 |
---|
|