ASP 三层架构 Convert类实现代码
来源:asp之家 发布时间:2011-03-16 11:01:00
这个类主要解决在类型转换时,如果直接使用类型转换函数,会因为变量为空或者格式不对而导致程序报错,而这种报错在大多数情况下是允许的.例如要转换一个字符串变量为数字,如果变量为空,则一般需要自动返回0.
另外一个重要功能就是封装变量格式化操作,可以保持整个网站的输出格式统一,例如时间格式,货币格式等等. 日期和货币格式化的时候,极易遇到因空值报错的情况,一般都不得不写个预判断空值的逻辑,再格式化变量. 使用这个类负责类型转换和格式化输出后,就不用操心这些琐碎的细节了,可以让编程的心情得到大大改善啊.
还有些其他格式化功能,也加了进去,例如Convert.ToPer()是用来转换数字成百分数,Convert.FirstUppercase()用来做首字母大写...... 你可以根据自己的需要,随时扩展这个类,不要忘记了和大家分享哦.
有些基本的函数,如果随便写一写,基本可以凑合着用,但是遇到特殊情况,就要重新改写.比如我写的Convert.ToInt()方法,将变量转换为Integer. 最基本的操作,是判断一下是否为空,不为空就直接用Cint()就可以了. 但是遇到变量超出了范围,又得判断是否在Integer范围内,所以又写了一个私有方法IsOverflowInteger(),用于判断变量值是否为某一个范围内的数字.经过这样的处理,相信基本可以处理所有的情况了.
所以我想,Convert类中的已有方法还是会有不少需要改善的,大家如果有更好更完善的函数请发上来分享,让它形成ASP中最标准的变量处理的类,再不用依赖ASP中那些有限的功能了.
如下列举一些比较主要的方法,具体细节请看代码.
类型判断:
Convert.IsInteger(ByVal Value) 判断是否整数,只允许0~9和-号
Convert.IsInt(ByVal Value) 判断是否int型,其下类似,不用解释了.
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)
类型转换:
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)
格式化:
Convert.FormatDat(ByVal Value, ByVal vStyle) 日期格式化
Convert.FormatCur(ByVal Value,ByVal vDecimal) 货币格式化
Convert.FormatNum(ByVal Value,ByVal vDecimal) 数字格式化
其他格式化:
Convert.ToPer(Byval value,Byval value0) 百分数,带%
Convert.FirstUppercase(ByVal value) 首字母大写
Convert.SafeSql(ByVal value) 替换sql中的'为''
代码如下: (我不会插入代码,不知道CSDN是怎么操作的,点插入代码就是一个<textarea>,而不是可以折叠代码的风格,向了解的朋友请教.)
代码如下:
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
' ****** 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
' ****** check if Value is in range of integer
' Only use in 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 while ( left(value0,1)="0" or left(value0,1)="-" )
value0=right(value0,len(value0)-1)
loop
do while ( 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
' **************************************
' ****** 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
' ****** check if Value is in range of Decimal
' Only use in 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
' ****** vDecimal : number of decimal places
Public Function ToCur(ByVal Value)
if IsCur(Value) then
ToCur=ccur(Value)
else
ToCur=0
end if
End Function
' ****** vType : format of 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)="JAN"
arrMonthArray(2)="FEB"
arrMonthArray(3)="MAR"
arrMonthArray(4)="APR"
arrMonthArray(5)="MAY"
arrMonthArray(6)="JUN"
arrMonthArray(7)="JUL"
arrMonthArray(8)="AUG"
arrMonthArray(9)="SEP"
arrMonthArray(10)="OCT"
arrMonthArray(11)="NOV"
arrMonthArray(12)="DEC"
select case intStyle
case 1
FormatDat=cstr(year(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(year(dateThis)) &"-"& cstr(month(dateThis)) &"-"& cstr(day(dateThis)) &" "& hour(dateThis) &":"& minute(dateThis) &":"& second(dateThis)
case 40
FormatDat=cstr(year(dateThis)) &"-"& ZeroPad(cstr(month(dateThis)),2) &"-"& ZeroPad(cstr(day(dateThis)),2)
case else
FormatDat=cstr(year(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.00%"
end if
End Function
' ****** value -> Value first code change to 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
猜你喜欢
- 代码很简单,只需要2行代码就可实现想要的功能,虽然很短,但确实使用,主要使用了requests库。测试2XX, 3XX, 4XX, 5XX都
- 需求说明当用户申请售后,商家未在n小时内处理,系统自动进行退款。商家拒绝后,用户可申请客服介入,客服x天内超时未处理,系统自动退款。用户收到
- 1.今天在看JavaScript学习指南的时候做的课后习题,也因此详细的对函数的传入参数进行比较深入的研究.题目如下:函数如何才能修改其作用
- 我写代码一直是这个风格,废话不多,直接给大家贴代码,现在还是老规矩,具体代码如下所示:using System;using System.C
- 自定义模板403<!DOCTYPE html><html lang="en"><head&
- 引用是指保存的值为对象的地址。在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心
- 第一种是用urllib模块,下面是例示代码:import urllibstatus=urllib.urlopen("https:/
- Payment Card Industry,即支付卡行业,PCI行业表示借记卡、信用卡、预付卡、电子钱包、ATM和POS卡及相关的业务。 P
- 一、知识介绍:1、input() 函数,接收任意输入,将所有输入默认为字符串处理,并返回字符串类型;2、可以用作文本输入,如用户名,密码框的
- (wuhen注:document.all是ie特有的属性,不是万维网标准) 当页面上的控件同名且多个的时候,你首先做的是什么?判断长度?的确
- 一、导出数据。 先说明一下自己的环境:Mac OS X 10.8.3, MySQL Community Server 5.6.10, MyS
- 背景一次工作中,我需要完成某个文件的字符串替换。需求是这样的:文件A有个占位符,需要利用Python3,把占位符替换成文件B的内容。文件都不
- // 格式化字符串 Fmt("{0}.[{id}].{name}",{id:1,name:'
- pycharm对数据库的查询等操作(非多步操作)import pymysqlimport pandas as pdconnect = pym
- 目录一.准备工作二.预览1.启动2.运行3.结果三.设计思路四.源代码4.1 GUI.py4.2 Search_Apps.py五.总结一.准
- 如果遇到与文件许可有关的问题,可能数启动mysqld时UMASK环境变量设置得不正确。例如,当你创建表时,MySQL可能会发出下述错误消息:
- 想找一个可以播放文字的闹钟找不到,自己写一个更简单。TTS实现由很多种办法,百度等都提供了API接口,但类似百度,需要先注册等一系列动作。其
- 冒泡的表现近期用vue做了一个需求,大概是同一个区域,点击不同位置有不同的响应函数,还有个总的响应函数,好吧,如下图所示:他们的DOM结构如
- 正则表达式中的符号例子 | 是或的关系,只要存在就会被捕获匹配到的数据只按字符串顺序返回,而不是按照匹配规则返回In [18]:
- 1.前言当我们创建Core项目的时候,Web根目录下会有个wwwroot文件目录,wwwroot文件目录里面默认有HTML、CSS、IMG、