如何实现人民币的大写转换?
第一个办法,这个程序可以进行万亿以下的货币金额转换(够用的了吧),其中汉字与数字均按一位计:
function atoc(a as currency) as string
' 定义两个字符串,a的值最多是两位小数.
dim string1 as string
' 如下定义.
dim string2 as string
' 如下定义.
dim string3 as string
' 从原a值中取出的值.
dim i as integer
' 循环变量.
dim j as integer
' a的值乘以100的字符串长度.
dim ch1 as string
' 数字的汉语读法.
dim ch2 as string
' 数字位的汉字读法.
dim nzero as integer
' 用来计算连续的非零数是几个.
string1 = "零壹贰叁肆伍陆柒捌玖"
string2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"
'msgbox cstr(a * 100)
if instr(1, cstr(a * 100), ".") <> 0 then
err.raise 5000, , "该函数( atoc() )只转换两位小数以内的数值!"
end if
j = len(cstr(a * 100))
string2 = right(string2, j)
' 取出对应位数的string2的值.
for i = 1 to j
string3 = mid(a * 100, i, 1)
' 取出需转换的某一位的值.
if string3 <> "0" then
ch1 = mid(string1, val(string3) + 1, 1)
ch2 = mid(string2, i, 1)
nzero = nzero + 1
' 表示本位不为零.
else
if nzero <> 0 or i = j - 9 or i = j - 5 or i = j - 1 then
if right(atoc, 1) = "零" then atoc = left(atoc, len(atoc) - 1)
ch1 = "零"
else
ch1 = ""
end if
if i = j - 10 then
' 如果转换的数值需要扩大,则要改动以下表达式 i 的值.
ch2 = "亿"
elseif i = j - 6 then
if nzero <> 0 then
ch2 = "万"
' nzero = 0
end if
elseif i = j - 2 then
ch2 = "元"
elseif i = j then
ch2 = "整"
else
ch2 = ""
end if
nzero = 0
end if
atoc = atoc & ch1 & ch2
next i
atoc = replace(atoc, "零元", "元")
atoc = replace(atoc, "零万", "万")
atoc = replace(atoc, "零亿", "亿")
atoc = replace(atoc, "零整", "整")
' 以上将多余的零去掉
end function
第二个办法:照下面写就成了!
<%
dim str(9)
str(0)="零"
str(1)="壹"
str(2)="贰"
str(3)="叁"
str(4)="肆"
str(5)="伍"
str(6)="陆"
str(7)="柒"
str(8)="捌"
str(9)="玖"
aa=request.form("source")
hh=formatnumber(aa,2,-1)
aa=replace(hh,".","")
aa=replace(aa,",","")
for i=1 to len(aa)
s=mid(aa,i,1)
mynum=str(s)
select case(len(aa)+1-i)
case 1: k= mynum&"分"
case 2: k= mynum&"角"
case 3: k= mynum&"元"
case 4: k= mynum&"拾"
case 5: k= mynum&"佰"
case 6: k= mynum&"仟"
case 7: k= mynum&"万"
case 8: k= mynum&"拾"
case 9: k= mynum&"佰"
case 10: k= mynum&"仟"
end select
m=m&k
next
%>
<html>
<head>
<title>精彩春风之数字大小写转换</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<!--
elseif(s=".") then
n=m
i=i+2
for j=i to len(aa)
s=mid(aa,i,1)
mynum=str(s)
select case(len(aa)+1-i)
case 1: p= mynum&"分"
case 2: p= mynum&"角"
end select
m=m&p
next
-->
<body>
<form method="post" name="forma">
<input type="text" name="source" value="<%=hh%>">
=
<input type="text" name="result" value="<%=m%>" size="40">
<input type="submit" name="submit" value="开始转换" >
</form>
</body></html>
上一篇: 学习凉面的做法,给夏日带来一丝凉意
下一篇: 我最多吃3个就吃不下了