The method of the ASP data forced conversion

Source: Internet
Author: User
Tags arithmetic numeric locale numeric value valid

  ASP Data casts

CBool (expression)

CByte (expression)

CCur (expression)

CDate (expression)

CDBL (expression) (keep decimal)

CDEC (expression)

CInt (expression) (no decimal)

CLng (expression) (no decimal)

CSng (expression)

CSTR (expression)

CVar (expression)

CSTR (expression)

VBScript type conversion function

  CBool function

The necessary expression parameter can be any string expression or numeric expression.

return type

The function name determines the return type, as follows:

function return type expression parameter range

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.

Any valid date expression for CDate date.

CDBL Double negative from -1.79769313486232E308 to -4.94065645841247E-324;

Number from 4.94065645841247E-324 to 1.79769313486232E308.

CDEC Decimal 0 Variable ratio, that is, no decimal digits, for

+/-79,228,162,514,264,337,593,543,950,335. For a value of 28 decimal digits, the range is

For

+/-7.9228162514264337593543950335 the smallest possible 0 value is 0.00000000000000

00000000000001.

CInt integer-32,768 to 32,767, fractional part rounded.

CLng long-2,147,483,648 to 2,147,483,647, fractional part rounded.

CSng single negative is -3.402823E38 to -1.401298E-45; positive number is 1.401298E-45

To 3.402823E38.

The CStr String returns CSTR based on the expression parameter.

If the CVar Variant is numeric, the range is the same as the Double; if it is not a numeric value, the range and the Stri

Ng the same.

Description

An error occurs if the expression passed to the function exceeds the range of the conversion target data type. Typically, you can use a data type conversion function when encoding to reflect that the results of some operations should be expressed as a specific data type, not the default data type. For example, when a single, double-precision, or integer operation occurs, use CCur to enforce currency operations.

You should use a data type conversion function instead of Val so that the international version of the data transformation can be converted from one data type to another. For example, when using Ccur, the different decimal separators, the thousand-bit separator, and various currency options are properly identified according to the country settings of the system.

When the decimal part is exactly 0.5, the Cint and CLNG functions convert it to the nearest even number. For example, 0.5 converts to 0, 1.5 to 2. The Cint and CLng functions are different from the fix and int functions, and the fix and INT functions truncate the fractional part rather than rounding it. And the Fix and INT functions always return the same value as the incoming data type.

Use the IsDate function to determine whether date can be converted to a date or time. Cdate can be used to identify date and time literals, as well as values that fall into an acceptable range of dates. When converting a number to a date, the integer part is converted to a date, and the decimal part is converted to the time from midnight.

CDate determines the format of dates according to the country settings on the system. If the format provided is an unrecognized date setting, the order of year, month, and day is not correctly judged. In addition, long date formats, if they contain a string of weeks, cannot be identified.

The Cvdate function also provides compatibility with earlier versions of Visual Basic. The syntax of the Cvdate function is exactly the same as the CDate function, however, Cvdate returns a Variant whose subtype is date instead of the actual date type. Because there are real Date types now, cvdate is no longer needed. Converting an expression to Date and assigning it to a Variant can also achieve the same effect. You can also use this technique to convert other real data types to equivalent Variant subtypes.

Pay attention to CDec

A function cannot return a separate data type, but always returns a Variant whose value has been converted to a Decimal subtype.

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.

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

  CSng function

Describe

Returns an expression that has been converted to a Variant of a single subtype.

Grammar

CSng (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 or integer operations, use CDBL or csng to force double or single precision operations.

The CSng function is used for internationally recognized format conversions from other data types to single subtypes. For example, the recognition of decimal separators, such as the thousand characters, depends on the locale of the system.

If expression is outside the range allowed by the single subtype, an error occurs.

The following example uses the CSng function to convert a value to single:

Dim MyDouble1, MyDouble2, MySingle1, MySingle2 ' MyDouble1, MyDouble2 is a double value.

MyDouble1 = 75.3421115:mydouble2 = 75.3421555

MySingle1 = CSng (MyDouble1) ' MySingle1 contains 75.34211.

MySingle2 = CSng (MyDouble2) ' MySingle2 contains 75.34216.

  CStr function

Describe

Returns an expression that has been converted to a Variant of String subtype.

Grammar

CSTR (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, use CSTR to force the result to be represented as a String.

The CSTR function is used to override the STR function for internationally recognized format conversions from other data types to String subtypes. For example, the recognition of a decimal separator depends on the locale of the system.

expression determines the data returned according to the following table:

If expression for CStr return

A Boolean string that contains True or False.

A date string that contains the system's short date format date.

Null run-time error.

Empty 0 Length string ("").

An error string that contains the word error following the error number.

Other numeric string that contains this number.

The following example converts a number to a String using the CSTR function:

Dim MyDouble, MyString

MyDouble = 437.324 ' MyDouble is a double value.

MyString = CStr (mydouble) ' MyString contains "437.324".

Related Article

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.