ACC2000: Rounding Errors When You Use Floating-Point Numbers

Article translations Article translations
Article ID: 210423 - View products that this article applies to.
This article was previously published under Q210423
This article has been archived. It is offered "as is" and will no longer be updated.
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).

Expand all | Collapse all

On This Page

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.

More information

Steps to Reproduce the Behavior

The following steps demonstrate the way floating point calculations may affect the final result:
  1. Start Microsoft Access, and then open any database.
  2. Press ALT+F11 to open the Visual Basic Editor.
  3. Press CTRL+G to open the Immediate window.
  4. Enter the following line, and then press ENTER:
    Print CInt(3.555 * 100)
    Note that the correct result of 356 is returned.

  5. Enter the following line, and then press ENTER:
    Print CInt(4.555 * 100)
    This calculation returns 455 instead or 456.

References

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

Properties

Article ID: 210423 - Last Review: October 26, 2013 - Revision: 3.0
Applies to
  • Microsoft Access 2000 Standard Edition
Keywords: 
kbnosurvey kbarchive kbprb kbprogramming KB210423

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com