变为大写金额的代码
已有 130 次阅读2008-1-26 14:52
Public Function UpNumber(ByVal Number As Double, Optional ByVal Typ As Long, Optional ByVal IsMoney As Boolean) As String
On Error GoTo Doerr
Dim Result As String
Dim strNumber As String
Dim lngNumberLen As Long
Dim strTmp As String
Dim strFirst As String, strEnd As String
Dim lngI As Long, lngJ As Long, lngTmp As Long
Dim strNum(10) As String
Dim strUnit(16) As String
Dim strUnitB(2) As String
Select Case Typ
Case 0
strNum(0) = "零": strNum(1) = "壹": strNum(2) = "贰": strNum(3) = "叁"
strNum(4) = "肆": strNum(5) = "伍": strNum(6) = "陆": strNum(7) = "柒"
strNum(8) = "捌": strNum(9) = "玖"
If IsMoney Then
strUnit(0) = "元"
strUnitB(0) = "角": strUnitB(1) = "分"
Else
strUnit(0) = "点"
End If
strUnit(1) = "拾": strUnit(2) = "佰": strUnit(3) = "仟": strUnit(4) = "万"
strUnit(5) = "拾": strUnit(6) = "佰": strUnit(7) = "仟": strUnit(8) = "亿"
strUnit(9) = "拾": strUnit(10) = "佰": strUnit(11) = "仟": strUnit(12) = "万"
strUnit(13) = "拾": strUnit(14) = "佰": strUnit(15) = "仟"
Case 1
strNum(0) = "零": strNum(1) = "一": strNum(2) = "二": strNum(3) = "三"
strNum(4) = "四": strNum(5) = "五": strNum(6) = "六": strNum(7) = "七"
strNum(8) = "八": strNum(9) = "九"
If IsMoney Then
strUnit(0) = "元"
strUnitB(0) = "角": strUnitB(1) = "分"
Else
strUnit(0) = "点"
End If
strUnit(1) = "十": strUnit(2) = "百": strUnit(3) = "千": strUnit(4) = "万"
strUnit(5) = "十": strUnit(6) = "百": strUnit(7) = "千": strUnit(8) = "亿"
strUnit(9) = "十": strUnit(10) = "百": strUnit(11) = "千": strUnit(12) = "万"
strUnit(13) = "十": strUnit(14) = "百": strUnit(15) = "千"
Case Else
GoTo Errexit
End Select
Result = ""
If Number = 0 Then
If IsMoney Then
Result = strNum(0) & strUnit(0) & "整"
Else
Result = strNum(0)
End If
Else
If IsMoney Then
strNumber = Trim(Str(FormatCurrency(Number, 2, vbTrue, vbFalse, vbFalse)))
Else
strNumber = Trim(Str(Number))
End If
lngNumberLen = Len(strNumber)
If Left(strNumber, 1) = "-" Then
strFirst = "负"
strNumber = Right(strNumber, lngNumberLen - 1)
lngNumberLen = lngNumberLen - 1
Else
strFirst = ""
End If
lngI = InStrRev(strNumber, ".")
If lngI Then
strTmp = Right(strNumber, lngNumberLen - lngI)
If IsMoney Then
strTmp = strTmp & "00"
strEnd = ""
For lngJ = 1 To 2
Result = Result & strNum(CLng(Mid$(strTmp, lngJ, 1))) & strUnitB(lngJ - 1)
Next
Else
strTmp = Right(strNumber, lngNumberLen - lngI)
For lngJ = 1 To lngNumberLen - lngI
Result = Result & strNum(CLng(Mid$(strTmp, lngJ, 1)))
Next
End If
strNumber = Left(strNumber, lngI - 1)
lngNumberLen = Len(strNumber)
Else
If IsMoney Then
strEnd = "整"
Else
strEnd = ""
End If
End If
lngI = 0
For lngJ = lngNumberLen To 1 Step -1
lngTmp = CLng(Mid$(strNumber, lngJ, 1))
If lngTmp Then
Result = strNum(lngTmp) & strUnit(lngI) & Result
Else
If lngI = 0 Or lngI = 4 Or lngI = 8 Or lngI = 12 Then
Result = strNum(lngTmp) & strUnit(lngI) & Result
Else
Result = strNum(lngTmp) & Result
End If
End If
lngI = lngI + 1
Next
Result = Replace(Result, strNum(0) & strNum(0), strNum(0))
Result = Replace(Result, strNum(0) & strNum(0), strNum(0))
Result = Replace(Result, strUnit(8) & strNum(0) & strUnit(4) & strNum(0) & strUnit(0), strUnit(8) & strUnit(0))
Result = Replace(Result, strUnit(8) & strNum(0) & strUnit(4), strUnit(8) & strNum(0))
Result = Replace(Result, strUnit(4) & strNum(0) & strUnit(0), strUnit(4) & strUnit(0))
Result = Replace(Result, strNum(0) & strUnit(8), strUnit(8))
Result = Replace(Result, strNum(0) & strUnit(4), strUnit(4))
Result = Replace(Result, strNum(0) & strUnit(0), strUnit(0))
Result = Replace(Result, strNum(0) & strNum(0), strNum(0))
Result = Replace(Result, strNum(0) & strNum(0), strNum(0))
If IsMoney Then
Result = strFirst & Result & strEnd
Else
Result = strFirst & Result
If Right(Result, 1) = strUnit(0) Then Result = Left(Result, Len(Result) - 1)
End If
End If
Complete:
GoTo Quit
Doerr:
Errexit:
Result = ""
Quit:
UpNumber = Result
End Function