Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Data Conversion Functions

After you determine what kind of value a Variant variable or a control holds, you can convert that argument to its associated datatype. The conversion functions shown in Table 14.7 describe the conversions you can perform.

Table 14.7. The data conversion functions.

Function Description
Asc() Converts its string argument to the ASCII number that matches the first (or only) character in the string
CCur() Converts the argument to an equivalent Currency datatype
CDbl() Converts the argument to an equivalent Double datatype
CInt() Rounds its fractional argument to the next highest integer
CLng() Converts the argument to an equivalentLong datatype
CSng() Converts the argument to an equivalent Single datatype
CStr() Converts the argument to an equivalent String datatype
CVar() Converts the argument to an equivalent Variant datatype
Fix() Truncates the fractional portion
Int() Rounds the number down to the integer less than or equal to its arguments
Hex() Converts its numeric argument to a hexadecimal (base-16) value
Oct() Converts its numeric argument to an octal (base-8) value

Hexadecimal is the base-16 number system. Octal is the base-8 number system.

Normally, the following assignment stores .1428571 in a label named lblValue:

lblValue.Caption = (1 / 7)

The following, however, adds precision to the label for a more accurate calculation to assign .142857142857143 to the label:

lblValue.Caption = CDbl(1 / 7)

Use these conversion functions when you need the exact datatype for more precision in calculations or controls.

Share ThisShare This

Informit Network