Each function can set the expression
(Expression: Any combination of arithmetic or logical operators, constants, functions and field names, controls, and attributes. The calculation result is a single value. Expressions can perform computation, Operation characters, or test data .) Forced conversion to specific
Data Type (Data Type: determines the field features of the data type that a field can possess. Data types include
Boolean, Integer, Long, Currency, Single, Double, Date, String, and Variant (default ).).
Syntax
CBool (expression)
CByte (expression)
CCur (expression)
CDate (expression)
CDbl (expression)
CDec (expression)
CInt (expression)
CLng (expression)
CSng (expression)
CStr (expression)
CVar (expression)
Required expression parameter (parameter: value that provides information for operations, events, methods, attributes, functions, or procedures .) Can be any string expression
(String expression: Any expression that evaluates to a column of continuous characters. The element of the expression can be: Return string or string Variant (VarType 8)
String Literal Value, constant, variable, or Variant .) Or numeric expression
(Numeric expression: Any expression that returns a number. Expressions can be any combination of variables, constants, functions, and operators .).
Return type
The function name determines the return type, as shown below:
Range of expression parameters of the function return type
Cbool Boolean any valid string or numeric expression.
Cbyte byte 0 to 255.
Ccur currency-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Cdate any valid date expression.
Cdbl double is negative,-1.79769313486231e308
-4.94065645841247e-324; for positive values, 4.94065645841247e-324 to 1.79769313486232e308.
Cdec decimal is an integer (that is, a number without a decimal number), and is +/-79,228,162,514,264,337,593,543,950,335. For digits with 28 decimal places, the value range is
+ //-7.9228162514264337593543950335. The possible minimum non-zero number is 0.0000000000000000000000000001.
CINT integer-32,768 to 32,767; decimal part is rounded.
Clng long-2,147,483,648 to 2,147,483,647; the fractional part is rounded down.
Csng single is negative,-3.402823e38 to-1.401298e-45; for positive values, 1.401298e-45 to 3.402823e38.
The return value of CSTR string CSTR depends on the Expression parameter.
CVar Variant has the same value range as that of double precision. For non-numeric values, the value range of String is the same.
Annotation
If the expression passed to the function is out of the value range of the target data type to be converted, an error occurs.
Generally, data types can be used in code.Conversion functionsTo indicate that the result of an operation should be a specific data type rather than the default data type. For example, the use of CCur forces currency operations where single-precision, double-precision, or integer operations usually occur.
Data type should be usedConversion functionsRather than Val to provide internationally recognized conversions between data types. For example, when CCur is used, different decimal points, different thousands of separators, and various currency options are correctly identified based on the computer's region.
When the decimal part is 0.5, both CInt and CLng always round it to the nearest even number. For example, 0.5 is rounded to 0, and 1.5 is rounded
2. The CInt and CLng functions are different from the Fix and Int functions. The latter truncates the fractional part of A number without rounding it. In addition, Fix and Int
Will always return the value of the same type as the input type.
You can use the IsDate function to determine whether a date can be converted to a date or a time. CDate
Recognize date text and time text, and some numbers in an acceptable date range. When you convert a number to a date, the integer is converted to a date. Any decimal part of the number will be converted into a day
Time (calculated from midnight ).
CDate is set based on the region of the system (Region settings: a group of information corresponding to the given language and country/region .) Recognize the date format. If the date value is provided in a format other than recognizable Date settings, the correct year, day, And month sequence may not be determined. In addition, if the long date format also contains a week string, this format cannot be recognized.
The CVDate function also provides compatibility with earlier versions of Visual Basic. Syntax of the CVDate function and CDate
The syntax of the function is the same. However, cvdate returns a variant value (its subtype is date) instead of the actual date type. Because there is an inherent
Date type. Therefore, cvdate is no longer required. You can convert the expression to date and assign it
Variant to achieve the same effect. This technique is consistent with the conversion from all other inherent types to their corresponding variant sub-types.
The comment cdec function does not return the discrete data type; instead, it always returns a variant whose value has been converted to a decimal subtype.
Example
Note the following example to demonstrate how to use this function in the Visual Basic for Applications (VBA) module. For more information about using VBA, select developer reference from the drop-down list next to search and enter one or more entries in the search box.
Cbool Functions
In this example, the cbool function is used to convert the expression to a Boolean value. If the expression evaluates to a non-zero value, cbool returns true; otherwise, it returns false.
Dim a, B, check
A = 5: B = 5'initialize variables.
Check = cbool (A = B) 'check contains true.
A = 0 'define variable.
Check = cbool (a) 'check contains false.
Cbyte Functions
In this example, the cbyte function is used to convert the expression to the byte value.
Dim mydouble, mybyte
Mydouble = 125.5678 'mydouble is a double.
Mybyte = cbyte (mydouble) 'mybyte contains 126.
Ccur Function
In this example, the ccur function is used to convert the expression to the currency value.
Dim mydouble, mycurr
Mydouble = 543.214588 'mydouble is a double.
Mycurr = ccur (mydouble * 2)
'Convert result of mydouble * 2 (1086.429176) to
'Currency (1086.4292 ).
Cdate Function
In this example, the cdate function is used to convert the string to the date value. Generally, it is not recommended to hard encode the date and time as strings (as shown in this example ). Use the date text and time text, for example, #2/12/1969 # And #4:45:23 #.
Dim mydate, my1_date, mytime, my1_time
Mydate = "February 12,196 9"
'Convert to date data type.
My1_date = cdate (mydate)
MyTime = "4:35:47"
'Convert to Date data type.
Mytimestamp = CDate (MyTime)
CDbl Functions
In this example, the CDbl function is used to convert the expression to a double-precision value.
Dim MyCurr, MyDouble
MyCurr = CCur (234.456784)
'Convert result to a Double.
MyDouble = CDbl (MyCurr * 8.2*0.01)
CDec Function
In this example, the CDec function is used to convert a value to a Decimal value.
Dim MyDecimal, MyCurr
MyCurr = 10000000.0587 'mycurr is a Currency.
MyDecimal = CDec (MyCurr) 'mydecimal is a Decimal.
CInt Function
In this example, the CInt function is used to convert a value to an Integer value.
Dim MyDouble, MyInt
MyDouble = 2345.5678 'mydouble is a Double.
MyInt = CInt (MyDouble) 'myint contains 2346.
Clng Function
In this example, the clng function is used to convert a value to a long value.
Dim myval1, myval2, mylong1, mylong2
Myval1 = 25427.45
Myval2 = 25427.55 'myval1, myval2 are doubles.
Mylong1 = clng (myval1)
'Mylong1 contains 25427.
Mylong2 = clng (myval2)
'Mylong2 contains 25428.
Csng Function
In this example, the csng function is used to convert a value to a single value.
Dim mydouble1, mydouble2, mysingle1, mysingle2
'Mydouble1, mydouble2 are doubles.
Mydouble1 = 75.3421115: mydouble2 = 75.3421555
Mysingle1 = csng (mydouble1)
'My single1 contains 75.34211.
Mysingle2 = csng (mydouble2)
'My single2 contains 75.34216.
CSTR Function
In this example, the CSTR function is used to convert a value to a string value.
Dim MyDouble, MyString
MyDouble = 437.324 'mydouble is a Double.
MyString = CStr (MyDouble)
'Mystring contains "437.324 ".
CVar Functions
In this example, the CVar function is used to convert the expression to the Variant value.
Dim MyInt, MyVar
MyInt = 4534 'myint is an Integer.
MyVar = CVar (MyInt & "000 ")
'The MyVar contains the string 4534000.