|
Function NumberToWord(inputnumber As Currency)
'2001年8月 lixibi
'本函数为财会数字转换为中文标准写法,设计思路优化算法,用最少的执行语句完成,
'For完成每个数字的转换
'转换后判断值为0时的处理,用Case判断不同位零的写法,可以让程序代码更易懂,优化程序执行
Const strChineseWord0 = "零"
Const strChineseWord1 = "壹"
Const strChineseWord2 = "贰"
Const strChineseWord3 = "参"
Const strChineseWord4 = "四"
Const strChineseWord5 = "伍"
Const strChineseWord6 = "陆"
Const strChineseWord7 = "柒"
Const strChineseWord8 = "捌"
Const strChineseWord9 = "玖"
On Error GoTo Err_NumberToWord
Dim intNumber As Byte, intDW As Byte, i As Byte
Dim strChineseNumber As String, strNumber As String
Dim intLong As Integer
strNumber = Trim(Str(inputnumber * 100)) '数值转换字符串,并且含分角的长度
intLong = Len(strNumber) '取得数值的长度
strChineseNumber = ""
For i = 1 To intLong
intDW = i
intNumber = Val(Mid(strNumber, intLong - i + 1, 1))
If intNumber = 0 Then '零位处理
Select Case I
Case 1, 2 '分,角位
If strChineseNumber <> "" Then
strChineseNumber = Numberchange(intNumber) & strChineseNumber
End If
Case 3, 7, 11 '元,万,亿位
strChineseNumber = dwchange(intDW) & strChineseNumber
Case Else '拾,佰,仟位
If Left(strChineseNumber, 1) = "元" Or Left(strChineseNumber, 1) = "万" Or Left(strChineseNumber, 1) = "亿" Then '如果左边是元万亿则不写
strChineseNumber = strChineseNumber
Else
If Left(strChineseNumber, 1) = "零" Then '如果是左边已有一个零则不加零,否则加零
strChineseNumber = strChineseNumber
Else
strChineseNumber = Numberchange(intNumber) & strChineseNumber
End If
End If
End Select
Else
strChineseNumber = Numberchange(intNumber) & dwchange(intDW) & strChineseNumber
End If
Next
If Right(strChineseNumber, 1) = "元" Or Right(strChineseNumber, 1) = "角" Then strChineseNumber = strChineseNumber & "整"
NumberToWord = strChineseNumber
Exit_NumberToWord:
Exit Function
Err_NumberToWord:
MsgBox Err.Description
Resume Exit_NumberToWord
End Function |
|