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
'如果轉換的數值需要擴大,那么需改動以下表達式 I 的值。 If I = J - 10 Then 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
|