当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP 三层架构 Convert类实现代码

ASP 三层架构 Convert类实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
这个类主要解决在类型转换时,如果直接使用类型转换函数,会因为变量为空或者格式不对而导致程序报错,而这种报错在大多数情况下是允许的.例如要转换一个字符串变量为数字,如果变量为空,则一般需要自动返回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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网