常有人問及錢或數字串要如何轉換為中文?由于轉過來是一個比較麻煩的事情。本人耐著性子把它搞定。
現將本人測試時的程序代碼貼上,我是將它們轉換為大寫的中文,希望大家用時方便一些。當然這是比較煩的了,不 要緊,慢慢看。呵呵......
代碼如下:
<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>
當然, 那位若有更好更簡便的方法, 可別忘了也告訴我一下, 讓我也進步進步。 fz_chenjl@163.net
OK !?!?
|