CDbl Function Returns Wrong Value with Currency Data Type

Last reviewed: July 29, 1997
Article ID: Q123186
The information in this article applies to:
  • Microsoft Visual Basic Programming System, Applications Edition, version 1.0

SYMPTOMS

In Microsoft Visual Basic, Applications Edition, the CDbl Function may return the incorrect value when you use a currency data type in the function.

CAUSE

This behavior occurs because the Cdbl function uses the incorrect conversion when you use a currency data type in the function.

For example, when you run the following procedure, the value 5.54975987041018E-18 is displayed in the message box, instead of the expected value 0:

   Sub Test()
      Dim cy As Currency, x As Double
      cy = 0.1
      x = cy
      MsgBox x - CDbl(cy)
   End Sub

In the above example, the CDbl function is used to convert the variable cy (Currency data type) to Double data type. If this occurred correctly, the result of subtracting Cdbl(cy) from x should be 0. This behavior occurs because the conversion used with Currency data type incorrectly preserves extra precision and the result is not zero.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

You can use the CDbl Function to convert any valid numeric or string expression to Double data type. For example, use CDbl to force double- precision arithmetic in cases where currency or integer arithmetic normally would occur.

Double data type values are stored as a 64-bit (8-byte) number ranging in value from -1.79769313486232E308 to -4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values.

REFERENCES

For more information about the CDbl Function, choose the Search button in the Visual Basic Reference, and type the following:

   CDbl
Keywords          : kbprg
Version           : 1.00
Platform          : WINDOWS


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.