'例子:
'debug.print upnumber(-10556765765555.45,0,true )
'显示为:
'负壹拾万伍仟伍佰陆拾柒亿陆仟伍佰柒拾陆万伍仟伍佰伍拾伍圆肆角零分
public function upnumber(byval number as double, optional byval typ as long, optional byval ismoney as boolean) as string
'********************************************************************************
'--------------------------------------------------------------------------------
'将阿拉伯数字转换为大写字符串
'version 1.0 2002-02-06
'version 1.1 2002-04-05 修改到支持到千亿
'version 1.2 2004-08-14 修改为支持 typ,ismoney 参数,转换结果可以不是金额,支持到百万亿
'roadbeg
'--------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------
'参数说明:
'number 待转换的数字,可以是小数.
'typ 转换类型,可选值 0,1
'0 转换为 零,壹,贰 等
'1 转换为 一,二,三 等
'ismoney 是否是金额,如果是,则转换为多少元,小数后转换为多少角,分,反之则转换为类似于"二点三"这种形式
'--------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------
'返回值说明:
'如果成功,返回转换后的字符串
'如果失败,返回空字符串
'--------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------
'注意,此函数最大只支持到百万亿
'没有对 typ 的值进行检查,如果 typ 不为 0,1 之一,将会引发错误.
'另,由于 double 类型数值范围的原因,超过百万亿,将不能显示小数,同样的超过十万亿只能显示一个小数,以此类推.
'--------------------------------------------------------------------------------
'********************************************************************************
on error goto doerr
dim result as string '返回值
dim strnumber as string '文本型的 number
dim lngnumberlen as long '文本型的 number 的 len
dim strtmp as string
dim strfirst as string, strend as string
dim lngi as long, lngj as long, lngtmp as long
dim strnum(10) as string '大写数字
dim strunit(16) as string '单位,比如 十,拾,万等
dim strunitb(2) as string '小数后的单位
'初始化
select case typ
case 0
strnum(0) = "零": strnum(1) = "壹": strnum(2) = "贰": strnum(3) = "叁": strnum(4) = "肆"
strnum(5) = "伍": strnum(6) = "陆": strnum(7) = "柒": strnum(8) = "捌": strnum(9) = "玖"
if ismoney then
strunit(0) = "圆"
strunitb(0) = "角": strunitb(1) = "分"
else
strunit(0) = "点"
end if
strunit(1) = "拾": strunit(2) = "佰": strunit(3) = "仟": strunit(4) = "万"
strunit(5) = "拾": strunit(6) = "佰": strunit(7) = "仟": strunit(8) = "亿": strunit(9) = "拾"
strunit(10) = "佰": strunit(11) = "仟": strunit(12) = "万": strunit(13) = "拾": strunit(14) = "佰"
strunit(15) = "仟"
case 1
strnum(0) = "零": strnum(1) = "一": strnum(2) = "二": strnum(3) = "三": strnum(4) = "四"
strnum(5) = "五": strnum(6) = "六": strnum(7) = "七": strnum(8) = "八": strnum(9) = "九"
if ismoney then
strunit(0) = "元"
strunitb(0) = "角": strunitb(1) = "分"
else
strunit(0) = "点"
end if
strunit(1) = "十": strunit(2) = "百": strunit(3) = "千": strunit(4) = "万"
strunit(5) = "十": strunit(6) = "百": strunit(7) = "千": strunit(8) = "亿": strunit(9) = "十"
strunit(10) = "百": strunit(11) = "千": strunit(12) = "万": strunit(13) = "十": strunit(14) = "百"
strunit(15) = "千"
case else
'参数错误
goto errexit
end select
result = ""
if number = 0 then
if ismoney then
result = strnum(0) & strunit(0) & "整"
else
result = strnum(0)
end if
else
if ismoney then
strnumber = trim(str(formatcurrency(number, 2, vbtrue, vbfalse, vbfalse))) '保留两位小数
else
strnumber = trim(str(number)) '简单的转换为字符串型
end if
lngnumberlen = len(strnumber)
if left(strnumber, 1) = "-" then '处理负数
strfirst = "负"
strnumber = right(strnumber, lngnumberlen - 1)
lngnumberlen = lngnumberlen - 1
else
strfirst = "" '通常不需要 =""
end if
lngi = instrrev(strnumber, ".")
if lngi then
strtmp = right(strnumber, lngnumberlen - lngi)
if ismoney then
strtmp = strtmp & "00"
strend = "" '通常不需要 =""
for lngj = 1 to 2
result = result & strnum(clng(mid$(strtmp, lngj, 1))) & strunitb(lngj - 1)
next
else
strtmp = right(strnumber, lngnumberlen - lngi)
for lngj = 1 to lngnumberlen - lngi
result = result & strnum(clng(mid$(strtmp, lngj, 1)))
next
end if
strnumber = left(strnumber, lngi - 1) '去除小数部分
lngnumberlen = len(strnumber) '新的字符串长度
else
if ismoney then
strend = "整"
else
strend = ""
end if
end if
'以下为主循环部分
lngi = 0
for lngj = lngnumberlen to 1 step -1
lngtmp = clng(mid$(strnumber, lngj, 1))
if lngtmp then
result = strnum(lngtmp) & strunit(lngi) & result
else
if lngi = 0 or lngi = 4 or lngi = 8 or lngi = 12 then '超过 16 位不支持
result = strnum(lngtmp) & strunit(lngi) & result
else
result = strnum(lngtmp) & result
end if
end if
lngi = lngi + 1
next
result = replace(result, strnum(0) & strnum(0), strnum(0)) '零零", "零
result = replace(result, strnum(0) & strnum(0), strnum(0)) '零零", "零
'亿零万零圆", "亿圆"
result = replace(result, strunit(8) & strnum(0) & strunit(4) & strnum(0) & strunit(0), strunit(8) & strunit(0))
result = replace(result, strunit(8) & strnum(0) & strunit(4), strunit(8) & strnum(0)) '亿零万, "亿零"
result = replace(result, strunit(4) & strnum(0) & strunit(0), strunit(4) & strunit(0)) '亿零万", "亿零
result = replace(result, strnum(0) & strunit(8), strunit(8)) '零亿
result = replace(result, strnum(0) & strunit(4), strunit(4)) '零万
result = replace(result, strnum(0) & strunit(0), strunit(0)) '零圆
result = replace(result, strnum(0) & strnum(0), strnum(0)) '零零", "零
result = replace(result, strnum(0) & strnum(0), strnum(0)) '零零", "零
if ismoney then
result = strfirst & result & strend
else
if right(result, 1) = strunit(0) then result = left(result, len(result) - 1) '去除最后一个 "点"
end if
end if
complete:
goto quit
doerr:
errexit:
result = ""
quit:
upnumber = result
end function
新闻热点
疑难解答