|
技术资料 > ASP技术 > 脚本编码 : 数字字串(钱)转换为中文(大写) |
数字字串(钱)转换为中文(大写) March 25,2004 |
常有人问及钱或数字串要如何转换为中文?由于转过来是一个比较麻烦的事情。本人耐着性子把它搞定。
现将本人测试时的程序代码贴上,我是将它们转换为大写的中文,希望大家用时方便一些。当然这是比较烦的了,不
要紧,慢慢看。呵呵......
代码如下:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<script language=vbscript>
<!--
dim aa,bb
function do_change(typeid)
Dim wordarr(11), unitarr1(4), unitarr2(4), unitarr3(3)
Dim int1 ' 整数部份
Dim point1 ' 小数部份
Dim int1_str ' 整数部份
Dim point1_str ' 小数部份
Dim str1, len1, i,j
Dim idx1, idx2,idx3, unit_tag
aa=testm.text1.value
If Not IsNumeric(aa) Then ' check error
msgbox "input is no number"
Exit Function
End If
if typeid = "money" then
str1 = FormatNumber(aa, 2, -1, 0)
else
str1 = FormatNumber(aa, 20, -1, 0)
end if
str1 = Replace(str1, ",", "")
len1 = Len(str1)
wordarr(1) = "零"
wordarr(2) = "壹"
wordarr(3) = "贰"
wordarr(4) = "参"
wordarr(5) = "肆"
wordarr(6) = "伍"
wordarr(7) = "陆"
wordarr(8) = "柒"
wordarr(9) = "捌"
wordarr(10) = "玖"
unitarr1(1) = "拾"
unitarr1(2) = "佰"
unitarr1(3) = "仟"
unitarr2(1) = "万"
unitarr2(2) = "亿"
unitarr2(3) = "兆"
unitarr3(1) = "角"
unitarr3(2) = "分"
j = 0
For i = 1 To len1
If mid(str1,i,1) = "." Then '将数字分成整数部分,及小数部分
j = i
Exit For
End If
Next
If j <> 0 Then
int1 = Mid(str1, 1, j - 1)
point1 = Mid(str1, j + 1, len1)
Else
int1 = str1
point1 = Null
End If
len1 = Len(int1)
If len1 = 0 Or int1 = "0" Then
int1_str = "零"
Else
j = 0
For i = len1 To 1 Step -1
j = j + 1
idx1 = mid(int1,j,1)
idx2 = (i - 1) / 4
idx3 = (i - 1) Mod 4
If idx3 = 0 Then
If idx1 <> 0 Then
int1_str = RTrim(int1_str) & wordarr(idx1 + 1)
unit_tag = "n"
End If
If idx2 <> 0 Then
If unit_tag <> "y" Then
int1_str = RTrim(int1_str) & unitarr2(idx2)
unit_tag = "y"
End If
End If
Else
If idx1 <> 0 Then
int1_str = RTrim(int1_str) & wordarr(idx1 + 1) & unitarr1(idx3)
unit_tag = "n"
Else
If int1(j + 1) <> 0 Then
int1_str = RTrim(int1_str) & wordarr(1)
unit_tag = "n"
End If
End If
End If
Next
End If
len1 = Len(point1)
If len1 = 0 Or point1 = "00" Then
point1_str = "零"
Else
If typeid = "money" Then
If mid(point1,1,1) = "0" Then
point1_str = "零"
End If
For p_1 = 1 To len1
idx1 = mid(point1,p_1,1)
If idx1 <> 0 Then
point1_str = RTrim(point1_str) & wordarr(idx1 + 1) & unitarr3(p_1)
End If
Next
else
For p_1 = len1 To 1 step -1
idx1 = mid(point1,p_1,1)
If idx1 <> 0 Then
point1_str = wordarr(idx1 + 1) & RTrim(point1_str)
end if
next
End If
End If
If typeid = "money" Then
If int1_str <> "零" Or j = 0 Then
bb = RTrim(int1_str) & "元"
End If
If point1_str <> "零" Then
bb = RTrim(bb) & RTrim(point1_str) & "整"
Else
bb = RTrim(bb) & "整"
End If
Else
bb = RTrim(int1_str)
If point1_str <> "零" and trim(point1_str) <>"" Then
bb = RTrim(bb) & "点" & RTrim(point1_str)
End If
End If
testm.text2.value = bb
End Function
-->
</script>
</HEAD>
<BODY>
<form method="post" name="testm" id="testm" action="testm.asp">
<P><INPUT id=text1 name=text1><br>
<INPUT id=button1 name=button1 type=button value="money" onclick="do_change('money')">
<INPUT id=button2 name=button2 type=button value="normal" onclick="do_change('normal')"><br>
<INPUT id=text2 name=text2 style="HEIGHT: 22px; WIDTH: 447px"></P>
</form>
</BODY>
</HTML>
当然, 那位若有更好更简便的方法, 可别忘了也告诉我一下, 让我也进步进步。 [email protected]
OK !?!?
|
|