Each function coerces an expression to a specific data type.
CCur(expression)
CDate(expression)
CDbl(expression)
CInt(expression)
CLng(expression)
CSng(expression)
CVar(expression)
CStr(expression)
The required argument expression is any String expression or numeric expression.
The function name determines the return type as shown in the following:
Function | Return Type | Range for expression argument |
---|---|---|
CCur | Currency | -922,337,203,685,477.5808 to 922,337,203,685,477.5807. |
CDate | Date | Any valid date expression |
CDbl | Double | -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. |
CInt | Integer | -32,768 to 32,767; fractions are rounded. |
CLng | Long | -2,147,483,648 to 2,147,483,647; fractions are rounded. |
CSng | Single | -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values. |
CVar | Variant | Same range as Double for numerics. Same range as String for non-numerics. |
CStr | String | Returns for CStr depend on the expression argument (as explained below). |
If the expression passed to the function is outside the range of the type being converted to, an error occurs.
In general, you can document your code using the data-type conversion functions to show that the result of some operation should be expressed as a particular data type rather than the default data type. For example, use CCur to force currency arithmetic in cases where single-precision, double-precision, or integer arithmetic normally would occur.
You should use the data-type conversion functions instead of Val to provide internationally-aware conversions from one data type to another. For example, when you use CCur, different decimal separators are properly recognized depending on the locale setting of your computer, as are different thousand separators and various currency options.
CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. Also, Fix and Int always return a value of the same type as is passed in.
Use the IsDate function to determine if date can be converted to a date or time. CDate recognizes date and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.
CDate recognizes date formats according to the locale setting of your system. The correct order of day, month, and year may not be determined if it is provided in a format other than one of the recognized date settings. In addition, a long date format is not recognized if it also contains the day-of-the-week string.