'-------------------------------------------------------------
'功能:小写金额转换为大写金额
'参数:
'返回:大写金额
' "0":整数位数超过9位
' "1":小数位数超过2位
' "零":num为0
'方法:text1.text=numtost(213123.03)
'-------------------------------------------------------------
Public Function numToStr(ByVal Num As Double) As String
Const strUnit = "元拾佰仟万拾佰仟亿拾佰仟"
Const strDigit = "零壹贰叁肆伍陆柒捌玖"
Dim strNum As String, strNumLeft As String, strNumRight As String
Dim i As Integer, iCount As Integer
Dim strFsbz As String '负数标志
'负数处理
If Left(Str(Num), 1) = "-" Then
strFsbz = "负"
Num = Mid(Str(Num), 2)
Else
strFsbz = ""
End If
'数值转换为字符串及错误处理
If Num = 0 Then
numToStr = "零"
Exit Function
End If
strNum = CStr(Num)
If InStr(strNum, "E") <> 0 Then
numToStr = "0"
Exit Function
End If
If InStr(strNum, ".") <> 0 Then
strNumLeft = Left(strNum, InStr(strNum, ".") - 1)
strNumRight = Right(strNum, Len(strNum) - InStr(strNum, "."))
'小数位数后只取2位
strNumRight = Left(strNumRight, 2)
Else
strNumLeft = strNum
strNumRight = ""
End If
If Len(strNumLeft) > 9 Then
numToStr = "0"
Exit Function
End If
If Len(strNumRight) > 2 Then
numToStr = "1"
Exit Function
End If
'转换大写金额
'转大写数码
For i = 0 To 9
strNumLeft = Replace(strNumLeft, i, Mid(strDigit, i + 1, 1))
strNumRight = Replace(strNumRight, i, Mid(strDigit, i + 1, 1))
Next i
'转换整数部分
'插入数位字符
iCount = Len(strNumLeft)
For i = 0 To iCount - 1
strNumLeft = Left(strNumLeft, iCount - i) & Mid(strUnit, i + 1, 1) & Right(strNumLeft, Len(strNumLeft) - iCount + i)
Next i
'(零仟、零佰、零拾变零)(零亿变亿,零万变万,零元变元)(零零变零)(亿万变亿)
For i = 2 To 4
While InStr(strNumLeft, "零" & Mid(strUnit, i, 1)) <> 0
strNumLeft = Replace(strNumLeft, "零" & Mid(strUnit, i, 1), "零")
End While
Next i
For i = 0 To 2
While InStr(strNumLeft, "零" & Mid(strUnit, i * 4 + 1, 1)) <> 0
strNumLeft = Replace(strNumLeft, "零" & Mid(strUnit, i * 4 + 1, 1), Mid(strUnit, i * 4 + 1, 1))
End While
Next i
While InStr(strNumLeft, "零零") <> 0
strNumLeft = Replace(strNumLeft, "零零", "零")
End While
strNumLeft = Replace(strNumLeft, "亿万", "亿")
'转换小数部分
Select Case Len(strNumRight)
Case 0
strNumRight = "整"
Case 1
strNumRight = strNumRight & "角整"
Case 2
strNumRight = Left(strNumRight, 1) & "角" & Right(strNumRight, 1) & "分整"
End Select
strNumRight = Replace(strNumRight, "零角", "零")
strNumRight = Replace(strNumRight, "零分", "")
strNumRight = Replace(strNumRight, "零整", "整")
strNumRight = Replace(strNumRight, "分整", "分")
numToStr = strNumLeft & strNumRight
If Left(numToStr, 1) = "零" Then numToStr = Right(numToStr, Len(numToStr) - 1)
If Left(numToStr, 1) = "元" Then numToStr = Right(numToStr, Len(numToStr) - 1)
numToStr = strFsbz + numToStr
End Function