This class is primarily resolved when type conversions are if you use a type conversion function directly, the program will be given an error because the variable is empty or incorrectly formatted, and this error is allowed in most cases. For example, to convert a string variable to a number, if the variable is empty, you generally need to return 0 automatically.
Another important function is to encapsulate variable formatting operations, you can maintain the entire site's output format unified, such as time format, currency format and so on. Date and currency format, it is very easy to encounter a null error in the case, generally have to write a predefined null value of the logic, and then format the variable. With this class responsible for type conversion and formatting output, you don't have to worry about these trivial details, so you can make your programming mood much better.
There are other formatting features that are added, such as Convert.toper () used to convert numbers to percentages, convert.firstuppercase () to capitalize the initials ... You can expand this class at any time according to your own needs, and don't forget to share it with you.
Some basic functions, if you write a write, basically can make use of, but encountered special circumstances, will be rewritten. Like I wrote the Convert.toint () method, converts a variable to an integer. The most basic operation, is to determine whether it is empty, not empty directly with CInt () on it. But when the variable is out of range and judged whether it is in the integer range, a private method, Isoverflowinteger (), is used to determine whether the variable value is a number in a range. After such treatment, I believe the basic can handle all the situation.
So I think that there are some methods in the Convert class will need to improve, if you have better and more perfect function please send to share, let it form the most standard variable processing ASP class, no longer rely on the ASP in those limited functions.
Listed below are some of the more important methods, see the code for details.
Type judgment:
Convert.isinteger (ByVal Value) determines whether integers, allowing only 0~9 and-numbers
Convert.isint (ByVal Value) determines whether the int type is similar to the next, without explanation.
CONVERT.ISLNG (ByVal Value)
Convert.isdecimal (ByVal Value)
Convert.issng (ByVal Value)
Convert.isdbl (ByVal Value)
Convert.iscur (ByVal Value)
Convert.isbln (ByVal Value)
Convert.isdat (ByVal Value)
Convert.isarr (ByVal Value)
Type conversions:
Convert.tostr (ByVal Value)
Convert.toint (ByVal Value)
CONVERT.TOLNG (ByVal Value)
Convert.tosng (ByVal Value)
Convert.todbl (ByVal Value)
Convert.tobln (ByVal Value)
Convert.tocur (ByVal Value)
Convert.todat (ByVal Value)
Format:
Convert.formatdat (ByVal Value, ByVal vstyle) date format
Convert.formatcur (ByVal value,byval vdecimal) currency format
Convert.formatnum (ByVal value,byval vdecimal) number format
Other formatting:
Convert.toper (Byval value,byval value0) percentage, with%
Convert.firstuppercase (ByVal value) first letter uppercase
Convert.safesql (ByVal value) replaces ' as ' in SQL
The code is as follows: (I do not insert code, do not know how csdn operation, the point of insertion code is a <textarea>, rather than can be folded code style, to understand friends to consult.)
Copy Code code as follows:
Class Con_convert
' ******global message
Private I,j,value0,value1,value2
Private Sub Class_Initialize
End Sub
Private Sub Class_Terminate
End Sub
' ==============================================================================
' Check Type, return Ture/false
' ==============================================================================
Public Function isstr (ByVal Value)
Isstr=true
End Function
' Hu Jintao Check string if is Integer
Public Function Isinteger (ByVal Value)
If Trim (value) = "" or IsNull (value) or IsEmpty (value) Then
Isinteger=false
Else
Isinteger = True
Value0=trim (Value)
For i = 1 to Len (VALUE0)
If ASC (Mid (Value0, I, 1)) >= ASC ("0") and ASC (Mid (Value0, I, 1)) <= ASC ("9") Then
Else
If ASC (Mid (VALUE0, I, 1)) = ASC ("-") and I=1 Then
Else
Isinteger = False
Exit for
End If
End If
Next
End If
End Function
' Hu Jintao Check if Value is in range of integer
' Only with this class
' Value:
' Vbound:max
Private Function Isoverflowinteger (ByVal value,byval vbound)
If Isinteger (Value) and Isinteger (Vbound) Then
Isoverflowinteger=false
Value0=trim (value)
Value1=trim (Vbound)
If Isoverflowinteger=false Then
' Delete 0 from left
Do as (left (value0,1) = "0" or left (value0,1) = "-")
Value0=right (Value0,len (VALUE0)-1)
Loop
Do as (left (value1,1) = "0" or left (value1,1) = "-")
Value1=right (Value1,len (value1)-1)
Loop
If Len (value0) =len (value1) Then
For I=1 to Len (VALUE0)
If ASC (Mid (value0,i,1) > Asc (Mid value1,i,1) or ASC (Mid value0,i,1) > ASC ("9") or ASC (Mid (value0,i,1)) < ASC ( "0") Then
Isoverflowinteger=true
Exit For
End If
Next
Else
If Len (value0) >len (value1) Then
Isoverflowinteger=true
End If
End If
End If
Else
Isoverflowinteger=true
End If
End Function
Public Function isint (ByVal Value)
Isint=true
If left (trim (value), 1) = "-" Then
If Isoverflowinteger (Trim (value), " -32768") Then
Isint=false
End If
Else
If Isoverflowinteger (Trim (value), "32767") Then
Isint=false
End If
End If
End Function
Public Function islng (ByVal Value)
Islng=true
If left (trim (value), 1) = "-" Then
If Isoverflowinteger (Trim (value), " -2147483648") Then
Islng=false
End If
Else
If Isoverflowinteger (Trim (value), "2147483647") Then
Islng=false
End If
End If
End Function
' **************************************
' Decimal
' **************************************
' Hu Jintao Check string if is Decimal
Private Function isdecimal (ByVal Value)
Dim Intdecimalcount
Intdecimalcount=0
If Trim (value) = "" or IsNull (value) or IsEmpty (value) Then
Isdecimal=false
Else
Isdecimal = True
Value0=trim (Value)
For i = 1 to Len (VALUE0)
If ASC (Mid (Value0, I, 1)) >= ASC ("0") and ASC (Mid (Value0, I, 1)) <= ASC ("9") Then
Else
Select Case ASC (Mid (Value0, I, 1))
Case ASC ("-")
If I=1 Then
Else
Isdecimal = False
Exit for
End If
Case ASC (".")
If Intdecimalcount<2 Then
Intdecimalcount=intdecimalcount + 1
Else
Isdecimal = False
Exit for
End If
Case Else
Isdecimal = False
Exit for
End Select
End If
Next
End If
End Function
' Hu Jintao Check if Value is in range of Decimal
' Only with this class
' Value:
' Vbound:
Private Function isoverflowdecimal (ByVal value,byval vbound)
If Trim (value) = "" or IsNull (value) or IsEmpty (value) or Trim (vbound) = "" or IsNull (Vbound) or IsEmpty (Vbound) Then
Isoverflowdecimal=true
Else
End If
End Function
Public Function issng (ByVal Value)
Issng=isdecimal (value)
'-340282300000000000000000000000000000000 ~-0.000000000000000000000000000000000000000000001401298
' 0.000000000000000000000000000000000000000000001401298 ~ 340282300000000000000000000000000000000
' -3.402823 E38 ~ -1.401298 E-45
' 1.401298 E-45 ~ 3.402823 E38
End Function
Public Function Isdbl (ByVal Value)
Isdbl=isdecimal (value)
' -1.79769313486232 E308 ~ -4.94065645841247 E-324
' 4.94065645841247 E-324 ~ 1.7976931348623 E308
End Function
Public Function iscur (ByVal Value)
Iscur=isdecimal (value)
'-922337203685477.5808 ~ 922337203685477.5807
End Function
Public Function isbln (ByVal Value)
If Value=true or Value=false or trim (value) = "1" or trim (value) = "0" Then
Isbln=true
Else
Isbln=false
End If
End Function
Public Function Isdat (ByVal Value)
If Trim (value) = "" or IsNull (value) or IsEmpty (value) Then
Isdat=false
Else
Isdat=isdate (Value)
End If
End Function
Public Function Isarr (ByVal Value)
If Trim (value) = "" or IsNull (value) or IsEmpty (value) Then
Isarr=false
Else
Isarr=isarray (Value)
End If
End Function
' ==============================================================================
' Convert Type, return value/initial value
' ==============================================================================
Public Function tostr (ByVal Value)
Tostr=trim (Value)
End Function
Public Function ToInt (ByVal Value)
If Isint (Value) Then
Toint=cint (Value)
Else
Toint=0
End If
End Function
Public Function tolng (ByVal Value)
If ISLNG (Value) Then
TOLNG=CLNG (Value)
Else
Tolng=0
End If
End Function
Public Function tosng (ByVal Value)
If Issng (Value) Then
Tosng=csng (Value)
Else
Tosng=0
End If
End Function
Public Function Todbl (ByVal Value)
If Isdbl (Value) Then
Todbl=cdbl (Value)
Else
Todbl=0
End If
End Function
Public Function tobln (ByVal Value)
If Isbln (Value) Then
Tobln=cbool (Value)
Else
Tobln=false
End If
End Function
' Places Vdecimal:number of decimal
Public Function tocur (ByVal Value)
If Iscur (Value) Then
Tocur=ccur (Value)
Else
Tocur=0
End If
End Function
' Vtype:format of the date
Public Function todat (ByVal Value)
If Isdat (Value) Then
Todat=cdate (value)
Else
Todat= ""
End If
End Function
' ==============================================================================
' Format
' ==============================================================================
' *******************************************************
' Formatdat
' Vdate
' Vstyle 0:2008-1-30 1:2008/1/30 2:1/30/2008 3:30/1/2008 4:30-jan-2008
' 10:2008-1 11:2008/1 12:1/2008
' 22:jan-2008
' 30:2008-1-30 11:20:20
' 40:2008-01-09
Public Function Formatdat (ByVal Value, ByVal Vstyle)
Dim Datethis,intstyle
Datethis=todat (Value)
Intstyle=toint (Vstyle)
If datethis= "" or IsNull (datethis) Then
Formatdat = ""
Else
Dim Arrmontharray (12)
Arrmontharray (1) = "A few"
Arrmontharray (2) = "FEB"
Arrmontharray (3) = "MAR"
Arrmontharray (4) = "APR"
Arrmontharray (5) = "May"
Arrmontharray (6) = "June"
Arrmontharray (7) = "June"
Arrmontharray (8) = "AUG"
Arrmontharray (9) = "SEP"
Arrmontharray (+) = "OCT"
Arrmontharray (one) = "NOV"
Arrmontharray (a) = "DEC"
Select Case Intstyle
Case 1
Formatdat=cstr (datethis) & "/" & CStr (Month (datethis)) & "/" & CStr (Day (datethis))
Case 2
Formatdat= CStr (Month (datethis)) & "/" & CStr (Day (datethis)) & "/" & CStr (Year (datethis))
Case 3
Formatdat= CStr (Day (datethis)) & "/" & CStr (Month (datethis)) & "/" & CStr (Year (datethis))
Case 4
Formatdat= CStr (Day (datethis)) & "-" & Arrmontharray (Month (datethis)) & "-" & CStr (Year (datethis))
Case 10
Formatdat=cstr (Year (datethis)) & "-" & CStr (Month (datethis))
Case 11
Formatdat=cstr (Year (datethis)) & "/" & CStr (Month (datethis))
Case 12
Formatdat= CStr (Month (datethis)) & "/" & CStr (Year (datethis))
Case 22
formatdat= Arrmontharray (Month (datethis)) & "-" & CStr (Year (datethis))
Case 30
Formatdat= CStr (datethis) & "-" & CStr (Month (datethis) & "-" & CStr (Day (datethis)) & "" & Hour (datethis) & ":" & Minute (Datethis) & ": & Second (datethis)
Case 40
Formatdat=cstr (datethis) & "-" & Zeropad (CStr (Month (datethis)), 2) & "-" & Zeropad (CStr ( Datethis)), 2)
Case Else
Formatdat=cstr (datethis) & "-" & CStr (Month (datethis)) & "-" & CStr (Day (datethis))
End Select
End If
End Function
' **************
' Formatcur
' **************
Public Function formatcur (ByVal value,byval vdecimal)
Formatcur=formatcurrency (Tocur (Value), ToInt (Vdecimal))
End Function
Public Function formatnum (ByVal value,byval vdecimal)
Formatnum=formatnumber (Todbl (Value), ToInt (Vdecimal))
End Function
' ==============================================================================
' Other format
' ==============================================================================
Public Function toper (Byval value,byval value0)
If Convert.todbl (VALUE0) <>0 Then
Toper = Me. Formatnum (Convert.todbl (value)/CONVERT.TODBL (VALUE0) * 100,2) & "%"
Else
Toper = "0%"
End If
End Function
'-> value ' uppercase
Public Function firstuppercase (ByVal value)
Value0 = Trim (value)
If Len (value0) =0 Then
Firstuppercase = ""
Else
Firstuppercase = UCase (left (value0,1)) & Right (Value0,len (VALUE0)-1)
End If
End Function
Public Function Safesql (ByVal value)
Safesql = replace (value, "'", "")
End Function
End Class