Access type Conversion Function

Source: Internet
Author: User

Each function can combine expressions (expressions: 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 a 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 .) It can be any string expression (string expression: an expression that evaluates to a column of continuous characters. The element of the expression can be: a function that returns a string or a string variant (vartype 8); A 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.

GenerallyCodeUse Data Types inConversion 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 to 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 always return values 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 identifies the date text and time text, and some numbers in the acceptable date range. When you convert a number to a date, the integer is converted to a date. Any decimal part of A number is converted to the time of the day (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. The syntax of the cvdate function is the same as that of the cdate function. However, cvdate returns a variant value (its subtype is date) instead of the actual date type. Because there is an inherent date type, cvdate is no longer required. You can convert the expression to date and assign it to 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.