Knowledge Base

CDbl Function Returns Wrong Value with Currency Data Type

PSS ID Number: 123186

Article Last Modified on 1/17/2001


The information in this article applies to:


This article was previously published under Q123186

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: KB123186
Technology: kbVBA100 kbVBASearch kbZNotKeyword3 kbZNotKeyword6