contents.gifindex.gifprev0.gifnext1.gif

Data Type Conversion Functions

Purpose

Explicitly converts expressions from one data type to another.

Syntax

CCur( expression )

CDbl( expression )

CInt( expression )

CLng( expression )

CSng( expression )

CStr( expression )

CVar( expression )

Notes

The argument expression can be any valid string expression or numeric expression.

The following table shows which data type is returned by each data type conversion function.

Function
From
To
CCur
Any valid expression
Currency
CDbl
Any valid expression
Double
CInt
Any valid expression
Integer
CLng
Any valid expression
Long
CSng
Any valid expression
Single
CStr
Any valid expression
String
CVar
Any valid expression
Variant

The numeric conversion functions CCur, CDbl, CInt, CLng, and CSng explicitly control the data type of a numeric expression. For example, you can use CCur to force currency arithmetic (which has greater precision but less range) in cases in which integer, double-precision, or single-precision arithmetic normally would occur. CInt and CLng force integer arithmetic in cases in which currency, single-precision, or double-precision arithmetic normally would occur. CDbl and CSng force double- or single-precision arithmetic in cases in which currency or integer arithmetic normally would occur.


Because CInt and CLng convert expressions to integers, you can use these functions to round fractional numbers.

Tip All data type conversion functions can provide self-documenting code that indicates that the result of a calculation should be expressed as a particular data type rather than the default data type of the result.

For the CCur, CInt, CLng, and CSng functions, if expression lies outside the acceptable range, a run-time error occurs and a message is displayed unless it is trapped in error-handling code.

Note CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. For a demonstration of the differences between Int and Fix, see the example for Int. When the fractional part is exactly 0.5, the CInt function always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.