VBScript type conversion function

Source: Internet
Author: User
Tags date format arithmetic expression functions integer numeric value valid
vbscript| Function | conversion CBool function

Describe
Returns an expression that has been converted to a Variant (variable) of a Boolean (Boolean type/subtype) subtype.
Grammar
CBool (expression)
expression is any valid expression.

Description
Returns False if expression is zero, otherwise returns TRUE. If expression cannot be interpreted as a numeric value, a run-time error occurs. The following example uses the CBool function to convert a expression (expression) to a Boolean. If expression is not zero, CBool returns TRUE, otherwise, returns FALSE.
Dim A, B, Check
A = 5:B = 5 ' initialization variable
Check = CBool (A = B) ' Check has a value of True

A = 0 ' define Variable
Check = CBool (A) ' checkbox set to False

CByte function

Describe
Returns an expression that has been converted to a Variant of the byte-type subtype.
Grammar
CByte (expression)
The expression parameter is any valid expression.

Description
In general, you can use a subtype conversion function to write code to show that the results of some operations should be represented as a specific data type, not the default type. For example, when currency, single-precision, double, or integer operations occur, use CByte to force byte operations.
The CByte function is used for internationally recognized format conversions from other data types to Byte subtypes. For example, the recognition of decimal separators, such as thousands, may depend on the locale of the system.

An error occurs if the expression is outside the acceptable range of Byte subtypes. The following example uses the CByte function to convert expression to byte:

Dim MyDouble, MyByte
MyDouble = 125.5678 ' MyDouble is a double value
MyByte = CByte (mydouble) ' MyByte contains 126

CCur function

Describe
Returns an expression that has been converted to a Variant of the Currency subtype.
Grammar
CCur (expression)
The expression parameter is any valid expression.

Description
Typically, you can use subtype data conversion functions to write code to show that the results of certain operations should be expressed as a specific data type, not as a default data type. For example, in the case of an integer operation, the CCur function is used to force a currency calculation.
The CCur function is used for internationally recognized format conversions from other data types to Currency subtypes. For example, the recognition of the decimal separator and the thousand separator depends on the locale of the system.


The following example uses the CCur function to convert expression to Currency:
Dim MyDouble, Mycurr
MyDouble = 543.214588 ' mydouble is double precision
Mycurr = CCur (MyDouble * 2) Converts the result of MyDouble * 2 (1086.429176) to
' Currency (1086.4292)


CDate function

Describe
Returns an expression that has been converted to a Variant of the Date subtype.
Grammar
CDate (date)
The date parameter is any valid datetime expression.

Description
The IsDate function is used to determine whether date can be converted to a date or time. CDate recognizes date and time literals, as well as some numbers within an acceptable date range. When you convert a number to a date, the integer portion of the number is converted to a date, and the fractional portion is converted to the time calculated from midnight.
CDate the date format according to the locale settings of the system. If the format of the data cannot be identified by the date setting, the correct order of year, month, and day cannot be judged. In addition, if the long date format contains a string that represents the day of the week, it cannot be recognized.

The following example uses the CDate function to convert a string to a date. In general, it is recommended that you do not use hard-coded dates and times (as shown in the following example). Instead, use the text form of the date and time (for example #10/19/1962#, #4:45:23 pm#).

MyDate = "October 19, 1962" ' defines date
Myshortdate = CDate (mydate) ' Convert to date data type
MyTime = "4:35:47 PM" defines time
Myshorttime = CDate (mytime) ' Convert to date data type

CDbl function

Describe
Returns an expression that has been converted to a Variant of the Double subtype.
Grammar
CDBL (expression)
The expression parameter is any valid expression.

Description
Typically, you can use subtype data conversion functions to write code to show that the results of certain operations should be expressed as a specific data type, not as a default data type. For example, in the case of currency or integer operations, use the CDBL or CSng function to force double or single-precision arithmetic operations.
The CDBL function is used for internationally recognized format conversions from other data types to Double subtypes. For example, the recognition of the decimal separator and the thousand separator depends on the locale of the system. The following example uses the CDBL function to convert expression to Double.

Dim Mycurr, MyDouble
Mycurr = CCur (234.456784) ' Mycurr is Currency type.
MyDouble = CDBL (Mycurr * 8.2 * 0.01) ' Converts the result to a Double type.


CInt function

Describe
Returns an expression that has been converted to a Variant of the Integer subtype.
Grammar
CINT (expression)
The expression parameter is any valid expression.

Description
In general, you can use a subtype conversion function to write code to show that the results of some operations should be represented as a specific data type, not the default type. For example, in the case of currency, single-precision, or double-precision operations, you use CINT or CLng to force an integer operation.
The CINT function is used for internationally recognized format conversions from other data types to Integer subtypes. For example, the recognition of decimal separators, such as thousands, may depend on the locale of the system.

If the expression is outside the acceptable range of the Integer subtype, an error occurs.

The following example uses the CINT function to convert a value to an Integer:

Dim MyDouble, MyInt
MyDouble = 2345.5678 ' MyDouble is Double.
MyInt = CInt (mydouble) ' MyInt contains 2346.


--------------------------------------------------------------------------------
Note that the CInt is different from the Fix and INT functions, and the latter both truncate the fractional portion of the number, rather than rounding it. When the fractional part is exactly 0.5, the CINT function usually rounds it to the nearest even number. For example, 0.5 is rounded to 0, and 1.5 is rounded to 2.
--------------------------------------------------------------------------------


CLng function

Describe
Returns an expression that has been converted to a Variant of a Long subtype.
Grammar
CLng (expression)
The expression parameter is any valid expression.

Description
Typically, you can use subtype data conversion functions to write code to show that the results of certain operations should be expressed as a specific data type, not as a default data type. For example, in the case of currency operations, single-precision, or double-precision arithmetic operations, the CINT or CLng functions are used to force integer operations.
The CLng function is used for internationally recognized format conversions from other data types to Long subtypes. For example, the recognition of the decimal separator and the thousand separator depends on the locale of the system.

If the expression value is not within the allowable range of a Long subtype, an error occurs.

The following example uses the CLng function to convert a value to Long:

Dim MyVal1, MyVal2, MyLong1, MyLong2
MyVal1 = 25427.45:myval2 = 25427.55 ' MyVal1, MyVal2 is a double value.
MyLong1 = CLng (MyVal1) ' MyLong1 contains 25427.
MyLong2 = CLng (MyVal2) ' MyLong2 contains 25428.


--------------------------------------------------------------------------------

Note that the CLng function differs from the FIX and INT functions, and the latter two functions truncate the decimal part of the value rather than rounding it. When the decimal part is exactly equal to 0.5, the CLng function is usually rounded to the nearest even number. For example, 0.5 rounding is 0, 1.5 rounding is 2.

-----------------------------------------------------------------------



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.