Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?



Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Focus On Visual Basic
A special Focus Section of Visual Basic Explorer

Part 4

<< Prev] [Next >>

String/numeric conversions

There is more to converting strings into numbers than will be mentioned here.  However, this can be a starting point for further investigation.

CStr$(expression)

Converts expression into a string.  Although expression is not limited to numeric values, for our purposes we will only focus on numeric values.
s = CStr$(123)   'Returns "123"
s = CStr$(12.3)  'Returns "12.3"
s = CStr$(-123)  'Returns "-123"
s = CStr$(.1)    'Returns "0.1"

Str$(numeric_expression)

Converts number_expression into a string.
s = Str$(123)   'Returns " 123"
s = Str$(12.3)  'Returns " 12.3"
s = Str$(-123)  'Returns "-123"
s = Str$(.1)    'Returns " .1"
Notice that converting non-negative numbers leaves a leading space.

Format$(expression[, format])

Converts expression into a formatted string based upon format.  If format is not provided or is "", the result is the same as CStr$(expression).  Although expression is not limited to numeric values, for our purposes we will only focus on numeric values.

There are two types of format values: named constants and user-defined formats.  A named constant applies a pre-defined format, whereas a user-defined format uses a special notation to define the format of the conversion.

Named Constant Description
"General Number" Displays at least one digit to the left of the decimal - the same as using CStr$.
"Currency" Prefixes a dollar sign, displays thousands separator and two decimal places.
"Fixed" Displays at least one digit to the left of the decimal and rounds to two decimal places.
"Standard" Displays thousands separator, at least one digit to the left of the decimal and rounds to two decimal places.
"Percent" Multiplies expression by 100, displays a trailing percent sign, at least one digit to the left of the decimal and rounds to two decimal places.
"Scientific" Displays in scientific notation.
"Yes/No" Displays non-zero values a "Yes", zero values as "No".
"True/False" Displays non-zero values a "True", zero values as "False".
"On/Off" Displays non-zero values a "On", zero values as "Off".
NOTE:  Named constants are not case sensitive: "Percent" provides the same formatting as "PERCENT".
Examples using named constants:
s = Format$(1234.567, "General Number")  'Returns "1234.567"
s = Format$(.1, "General Number")  'Returns "0.1"
s = Format$(1234.567, "Currency")  'Returns "$1,234.57"
s = Format$(0, "Currency")         'Returns "$0.00"
s = Format$(-1234.567, "Fixed")    'Returns "-1234.57"
s = Format$(0, "Fixed")            'Returns "0.00"
s = Format$(1234.567, "Standard")  'Returns "1,234.57"
s = Format$(0, "Standard")         'Returns "0.00"
s = Format$(.1, "Percent")         'Returns "10.00%"
s = Format$(-4.56789, "Percent")   'Returns "-456.79%"
s = Format$(0, "Percent")          'Returns "0.00%"
s = Format$(.1, "Scientific")      'Returns "1.00E-01"
s = Format$(-.1, "Scientific")     'Returns "-1.00E-01"
s = Format$(456.78, "Scientific")  'Returns "4.57E+02"
s = Format$(0, "Scientific")       'Returns "0.00E+00"
s = Format$(123, "Yes/No")         'Returns "Yes"
s = Format$(-.123, "Yes/No")       'Returns "Yes"
s = Format$(0, "Yes/No")           'Returns "No"
s = Format$(123, "True/False")     'Returns "True"
s = Format$(-.123, "True/False")   'Returns "True"
s = Format$(0, "True/False")       'Returns "False"
s = Format$(123, "On/Off")         'Returns "On"
s = Format$(-.123, "On/Off")       'Returns "On"
s = Format$(0, "On/Off")           'Returns "Off"
The special characters in format that are used in user-defined formatting are:
Character Description
0 A digit placeholder.  Will always display a digit.
# A digit placeholder.  Will only display a significant digit.  That is to say, it does not print leading and trailing zeros.
. Decimal placeholder.
, Thousands separator.  It must be surrounded by "#" and "0" characters.
% Percent placeholder.  Expression is multiplied by 100.
Examples using user-defined formats:
s = Format$(0, "#")              'Returns ""
s = Format$(0, "0")              'Returns "0"
s = Format$(1, "0000.00")        'Returns "0001.00"
s = Format$(-1, "0000.00")       'Returns "-0001.00"
s = Format$(1234.567, "0.000")   'Returns "1234.567"
s = Format$(1234.567, "#,0.00")  'Returns "1,234.57"
s = Format$(.12345, "0.00%")     'Returns "12.35%"
This is not an exhaustive treatment of the Format$ function.  For further details, refer to VB's help facility.

IsNumeric(expression)

Returns True if expression contains a numeric expression; otherwise, False is returned.  Although expression is not limited to string values, for our purposes we will only focus on string values.
f = IsNumeric("123")   'Returns True
f = IsNumeric("1 2")   'Returns False (embedded space)
f = IsNumeric("&HA")   'Returns True (hexidecimal notation)
f = IsNumeric("&O17")  'Returns True (octal notation)
f = IsNumeric("3E4")   'Returns True (scientific notation)
f = IsNumeric("12E")   'Returns False
f = IsNumeric("-12")   'Returns True
f = IsNumeric("12-")   'Returns True (trailing sign allowed)

Val(string)

Converts as much of string into a number as possible.  The conversion stops when a character of string is not recognized as part of a numeric value.
n = Val("123ABC")  'Returns 123
n = Val("12.3.5")  'Returns 12.3
n = Val("-1 2 3")  'Returns -123
n = Val("3E-3")    'Returns 0.003 (scientific notation)
n = Val("&H10")    'Returns 16 (hexidecimal conversion)
n = Val("Junk")    'Returns 0 (no recognized numeric value)
For additional, more robust conversions from strings to numeric values, refer to VB's help facility for information about the functions CInt, CLng, CSng, CDbl, CCur and CDec.

<< Prev] [Next >>





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.