Analysis on ASP. NET data type conversion

Source: Internet
Author: User

How does ASP. NET convert data types? Let's first look at the involved functions:

Each function can forcibly convert an expression to a specific data type.

ASP. NET data type conversion syntax

CBool (expression)

CByte (expression)

CCur (expression)

CDate (expression)

CDbl (expression)

CDec (expression)

CInt (expression)

CLng (expression)

CSng (expression)

CStr (expression)

CVar (expression)

CStr (expression)

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

ASP. NET data type conversion return type

The function name determines the return type, as shown below:

ASP. NET data type conversion 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.

CDate any valid Date expression.

CDbl Double negative number from-1.79769313486232E308 to-4.94065645841247E-324; positive number from 4.94065645841247E-324 to 1.79769313486232E308.

CDec Decimal zero-to-zero ratio value, that is, the number of Decimal places is +/-79,228,162,514,264,337,593,543,950,335. For 28 decimal places, the value range is +/-7.9228162514264337593543950335. The minimum possible non-zero value is 0.0000000000000000000000000001.

CInt Integer-32,768 to 32,767, rounded to the decimal part.

CLng Long-2,147,483,648 to 2,147,483,647, rounded to the decimal part.

The negative CSng Single number is-3.402823E38 to-1.401298E-45, and the positive number is 1.401298E-45 to 3.402823E38.

CStr String returns Cstr based on the expression parameter.

If CVar Variant is a value, the range is the same as that of Double. If it is not a value, the range is the same as that of String.

ASP. NET data type conversion

If the expression passed to the function exceeds the conversion target data type range, an error occurs. Generally, you can use a data type conversion function during encoding to indicate that the result of some operations should be represented as a specific data type, rather than the default data type. For example, when single-precision, double-precision, or integer operations occur, CCur is used to force the currency operation.

The data type conversion function should be used to replace Val, so that the international version of data conversion can be converted from one data type to another. For example, when Ccur is used, different decimal separator, kilobytes separator, and various currency options are properly identified based on the country settings of the system.

When the decimal part is 0.5, The Cint and CLng functions convert it to the closest even value. For example, 0.5 is converted to 0, and 1.5 is converted to 2. The Cint and CLng functions are different from the Fix and Int functions. 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 input data type.

Use the IsDate function to determine whether a date can be converted to a date or a time. Cdate can be used to identify date text and time text, as well as numbers falling into the acceptable date range. When a number is converted 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 date format based on the country settings on the system. If the format provided is an unrecognized date setting, the order of the year, month, and day cannot be correctly determined. In addition, the long date format cannot be recognized if it contains a string of weeks.

The CVDate function also provides compatibility with earlier Visual Basic versions. The syntax of the CVDate function is exactly the same as that of the CDate function. However, CVDate returns a Variant with a subtype of Date instead of the actual Date type. CVDate is no longer needed because the current Date type already exists. Convert an expression to Date and assign a value to Variant, which can achieve the same effect. You can also use this technique to convert other real data types into equal Variant subtypes.

Note that the CDec function cannot return an independent data type, but always returns a Variant. Its value has been converted to a Decimal subtype.

Cint () is a function in ASP.

Function converts an expression to a numeric type

ASP. NET data type conversion expression CInt (expression)

Allowed data type: Any valid characters are allowed.

ASP. NET data type conversion instance:

 
 
  1. ﹤%  
  2. f = "234" 
  3. response.write cINT(f) + 2  
  4. %﹥ 

Returned result: 236

The conversion character "234" is a number "234". If the string is null, 0 is returned.

We often see overflow errors of cint and clng. If a function is detected, this problem will not occur. The following two functions are written at the request of a friend. Let's take a look.

 
 
  1. 'Check whether the string is an integer.
  2. Function Is_Int (a_str)
  3. IfNot isnumeric (a_str) or len (str)> 5 then
  4. Is_Int =False
  5. Exit function
  6. Elseif len (str) <5 then
  7. Is_Int =True
  8. Exit function
  9. EndIf
  10. IfCint (left (a_str, 4) & gt; 3276 then
  11. Is_Int =False
  12. Exit function
  13. Elseif cint (left (a_str, 4) = 3276 and cint (right (a_str, 1)> 7 then
  14. Is_Int =False
  15. Exit function
  16. Else
  17. Is_Int =True
  18. Exit function
  19. EndIf
  20. End function
  21.  
  22. 'Check whether it is a long integer
  23. Function Is_Lng (a_str)
  24. IfNot isnumeric (a_str) or len (str)> 10 then
  25. Is_Lng =False
  26. Exit function
  27. Elseif len (str) <10 then
  28. Is_Lng =True
  29. Exit function
  30. EndIf
  31. IfClng (left (a_str, 9)> 214748367 then
  32. Is_Lng =False
  33. Exit function
  34. Elseif clng (left (a_str, 9) = 214748367 and clng (right (a_str, 1)> 7 then
  35. Is_Lng =False
  36. Exit function
  37. Else
  38. Is_Lng =True
  39. Exit function
  40. EndIf
  41. End function

ASP. NET data type conversion information will be introduced here, I hope you have some understanding of ASP. NET data type conversion.

  1. Use of SqlHelperSqlHelper in ASP. NET data access layer
  2. Hash Algorithm for ASP. NET Data Encryption
  3. Implement ASP. NET data encryption using symmetric encryption algorithms
  4. ASP. NET data import implementation Excel to MSSQL
  5. Example of ASP. NET dataset usage

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.