Error Converting String to Double When Using "%"

Last reviewed: February 9, 1996
Article ID: Q145695
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, version 4.0, for Windows

SUMMARY

When converting a string with a numeric representation to a double with a "%" percent symbol, a type mismatch is generated. In Visual Basic 3.0, the application implicitly understood the "%" and converted the output to its decimal equivalent

Step-by-Step Example

Use the steps below to re-create the error:

  1. Start a new Visual Basic project in either the Visual Basic 16-bit or 32- bit Edition.

  2. Add a label to Form1. In the caption property of Label1, put in the number "12.00%". Add a command button to the form, and in the click event place the following:

       Private Sub Command1_Click()
       Dim D as Double
       D = CDbl(Label1.Caption)
       Msgbox Str$(D)
       End sub
    
    
The following code will generate a type mismatch error in both Visual Basic 16-bit and 32-bit versions. In Visual Basic 3.0, the string will be implicitly converted to ".12".

  1. For this code to work, the Left function should be implemented to strip off the "%" character. In the case of the previous example, the output needs to be divided by 100.

       Private Sub Command1_Click()
       Dim D as Double
       D = CDbl(Left$(Label1.Caption,Len(Label1.Caption) - 1)/100)
       Msgbox Str$(D)
       End sub
    
    
The code above with the use of the Left function will output ".12".


Additional reference words: 4.00 vb4win
KBCategory: kbprg
KBSubcategory: PrgOther


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: February 9, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.