DOCUMENT:Q174155 24-SEP-1997 [vbwin] TITLE :BUG: Format Function May Return Incorrect Values PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0 OPER/SYS:WINDOWS KEYWORDS:PrgOther VB4ALL VB4WIN vb5all VBKBProgramming ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0 - Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0 --------------------------------------------------------------------- SYMPTOMS ======== Decimal values rounded by the Format function may be incorrect. For a specific decimal number, the rounded value returned by the Format function may differ for different data types and between different versions of Microsoft Visual Basic. CAUSE ===== When a variable is passed into the Format function, an algorithm is used to determine the most appropriate numeric data-type to convert the value to, and how to format it. This algorithm may not evaluate certain decimal values as expected. RESOLUTION ========== To avoid these unexpected conversions, explicitly convert the value to the desired data type before using the Format function. For instance, use the CCur function to convert the decimal value to a currency data-type that maintains more accuracy in rounding. STATUS ====== Microsoft has confirmed this to be a bug 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 ================ Decimal values stored in single and double data-types are binary representations of the number. Certain decimal values cannot be exactly represented in binary and are approximately represented. When evaluated by the Format function, these values may not be rounded as expected. There are two methods recommended to minimize these errors: 1. Use the Currency data type rather than singles or doubles. Currency values are actually stored as integers with the decimal point scaled four places. 2. Convert the values to Currency before evaluation by the Format function. Steps to Reproduce Behavior --------------------------- 1. In Microsoft Visual Basic, create a new Standard EXE project. Form1 is created by default. 2. On Form1, add a textbox (Text1) and three labels (Label1, Label2, and Label3). 3. Set the Text property of Text1 to a zero and the Caption property of each of the labels to nothing. 4. Add the following code to the Change event of the textbox: Dim dblValue As Double Dim sngValue As Single Dim curValue As Currency dblValue = Text1.Text sngValue = Text1.Text curValue = Text1.Text Label1.Caption = Format(dblValue, "#.000") Label2.Caption = Format(sngValue, "#.000") Label3.Caption = Format(curValue, "#.000") 5. Run the project, and enter some decimal values in the textbox, such as; 0.9125 0.7125 0.0915. Note that the values displayed in the labels are different. The Format function rounds some values up and some down based upon the data-type. For instance, 0.9125 may be displayed as .912 and .913. To correct this problem and to keep all values consistent, do one of the following: 1. Use the CCur() function to convert the values to a Currency data type before using the Format() function; TextDblCur = Format(CCur(dblValue), "#.000") TextSngCur = Format(CCur(sngValue), "#.000") 2. Declare the variables as Currency. REFERENCES ========== For additional information, please see the following articles in the Microsoft Knowledge Base: ARTICLE-ID: Q42980 TITLE : (Complete) Tutorial to Understand IEEE Floating-Point Errors ARTICLE-ID: Q69333 TITLE : HOWTO: Work Around Floating-Point Accuracy/Comparison Problems Additional query words: Precision ====================================================================== Keywords : PrgOther VB4ALL VB4WIN vb5all VBKBProgramming Version : WINDOWS:4.0,5.0 Platform : WINDOWS Issue type : kbbug ============================================================================= 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. Copyright Microsoft Corporation 1997.