|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Option Explicit
- Private Const ChnGenText As String = "零一二三四五六七八九"
- Private Const ChnRMBText As String = "零壹贰叁肆伍陆柒捌玖"
- Private Const ChnGenDigit As String = "十百千万亿"
- Private Const ChnRMBDigit As String = "拾佰仟万亿"
- Private Const ChnRMBUnit As String = "角分"
- ''' <summary>
- ''' 主转换函数
- ''' </summary>
- ''' <param name="strDigit">待转换数字字符串</param>
- ''' <param name="bToRMB">是否转换成人民币</param>
- ''' <returns>转换成的大写字符串</returns>
- Public Function ConvertChn(ByVal strDigit As String, Optional ByVal bToRMB As Boolean = False) As String
- Dim ErrStr As String
- If CheckDigit(strDigit, bToRMB, ErrStr) Then ' 检查输入数字有效性
- If bToRMB Then
- Dim indexOfPoint As Integer
- indexOfPoint = InStr(strDigit, ".")
- If indexOfPoint Then strDigit = Round(Val(strDigit), 2)
- End If
- Dim strResult As String
- Call ExtractSign(strResult, strDigit, bToRMB)
- Call ConvertNumber(strResult, strDigit, bToRMB)
- ConvertChn = strResult
- Else
- ConvertChn = ErrStr
- End If
- End Function
-
- Private Sub ConvertNumber(ByRef strResult As String, ByVal strDigit As String, ByVal bToRMB As Boolean)
- Dim indexOfPoint As Integer
- indexOfPoint = InStr(strDigit, ".")
- If indexOfPoint = 0 Then
- strResult = strResult & ConvertIntegral(strDigit, bToRMB)
- If bToRMB Then strResult = strResult & "圆整"
- Else
- If indexOfPoint = 1 Then
- If Not bToRMB Then strResult = strResult & "零"
- Else
- strResult = strResult & ConvertIntegral(Left(strDigit, indexOfPoint - 1), bToRMB)
- End If
- If Len(strDigit) <> indexOfPoint Then
- If bToRMB Then
- If indexOfPoint <> 1 Then
- If Len(strResult) = 1 And strResult = "零" Then
- strResult = Replace(strResult, "零", "")
- Else
- strResult = strResult & "圆"
- End If
- End If
- Else
- strResult = strResult & "点"
- End If
- Dim strTmp As String
- strTmp = ConvertFractional(Mid(strDigit, indexOfPoint + 1, Len(strDigit)), bToRMB)
- If Len(strTmp) <> 0 Then
- If bToRMB And Len(strResult) = 0 And Left(strTmp, 1) = "零" Then
- strResult = strResult & Mid(strTmp, 2, Len(strTmp))
- Else
- strResult = strResult & strTmp
- End If
- End If
- If bToRMB Then
- If Len(strResult) = 0 Then
- strResult = strResult & "零圆整"
- ElseIf Right(strResult, 1) = "圆" Then
- strResult = strResult & "整"
- End If
- End If
- ElseIf bToRMB Then
- strResult = strResult & "圆整"
- End If
- End If
- End Sub
-
- Private Function CheckDigit(ByRef strDigit As String, ByVal bToRMB As Boolean, Optional ByRef ErrStr As String = "") As Boolean
- Dim isValidate As Boolean
- If IsNumeric(strDigit) Then
- isValidate = True
- Else
- ErrStr = "输入数字的格式不正确!"
- isValidate = False
- End If
- If bToRMB Then
- If Val(strDigit) >= 1E+16 Then
- ErrStr = "输入数字太大,超出转换范围!"
- isValidate = False
- ElseIf Val(strDigit) < 0 Then
- ErrStr = "不允许人民币为负值!"
- isValidate = False
- End If
- Else
- If Val(strDigit) <= -1E+16 Or Val(strDigit) >= 1E+16 Then
- ErrStr = "输入数字太大或太小,超出转换范围!"
- isValidate = False
- Else
- isValidate = True
- End If
- End If
- CheckDigit = isValidate
- End Function
-
- Private Sub ExtractSign(ByRef strResult As String, ByRef strDigit As String, ByVal bToRMB As Boolean)
- If Left(strDigit, 1) = "+" Then
- strDigit = Mid(strDigit, 2, Len(strDigit))
- ElseIf Left(strDigit, 1) = "-" Then
- If Not bToRMB Then strResult = strResult & "负"
- strDigit = Mid(strDigit, 2, Len(strDigit))
- ElseIf Right(strDigit, 1) = "+" Then
- strDigit = Left(strDigit, Len(strDigit) - 1)
- ElseIf Right(strDigit, 1) = "-" Then
- If Not bToRMB Then strResult = strResult & "负"
- strDigit = Left(strDigit, Len(strDigit) - 1)
- End If
- End Sub
-
- Private Function ConvertIntegral(ByVal strIntegral As String, ByVal bToRMB As Boolean) As String
- Dim i As Integer, j As Integer, mylen As Integer, digit As Integer, mymod As Integer, index As Integer
- Dim strInt As String, chnText As String, chnDigit As String, strTemp As String
- Dim bDoSomething As Boolean
- mylen = Len(strIntegral)
- digit = mylen - 1
- chnText = IIf(bToRMB, ChnRMBText, ChnGenText)
- chnDigit = IIf(bToRMB, ChnRMBDigit, ChnGenDigit)
- For i = 1 To mylen - 1
- index = Val(Mid(strIntegral, i, 1)) + 1
- strInt = strInt & Mid(chnText, index, 1)
- mymod = digit Mod 4
- If mymod = 0 Then
- If digit = 4 Or digit = 12 Then
- strInt = strInt & Mid(chnDigit, 4, 1)
- ElseIf digit = 8 Then
- strInt = strInt & Mid(chnDigit, 5, 1)
- End If
- Else
- strInt = strInt & Mid(chnDigit, mymod, 1)
- End If
- digit = digit - 1
- Next
- index = Val(Mid(strIntegral, mylen, 1)) + 1
- If Right(strIntegral, 1) <> "0" Or mylen = 1 Then strInt = strInt & Mid(chnText, index, 1)
- i = 0
- skip:
- Do While i < Len(strInt)
- j = i
- bDoSomething = False
- Do While j < Len(strInt) - 1 And Mid(strInt, j + 1, 1) = "零"
- strTemp = Mid(strInt, j + 2, 1)
- If Mid(chnDigit, 4, 1) = strTemp Or Mid(chnDigit, 5, 1) = strTemp Then
- bDoSomething = True
- Exit Do
- End If
- j = j + 2
- Loop
- If j <> i Then
- strInt = Left(strInt, i) & Mid(strInt, j + 1)
- If i <= Len(strInt) - 1 And Not bDoSomething Then
- strInt = Left(strInt, i) & "零" & Mid(strInt, i + 1)
- i = i + 1
- End If
- End If
- If bDoSomething Then
- strInt = Left(strInt, i) & Mid(strInt, i + 2)
- i = i + 1
- GoTo skip
- End If
- i = i + 2
- Loop
- strTemp = Mid(chnDigit, 5, 1) & Mid(chnDigit, 4, 1)
- index = InStr(strInt, strTemp)
- If index <> 0 Then
- If Len(strInt) - 1 <> index And index + 1 < Len(strInt) And Mid(strInt, index + 2, 1) <> "零" Then
- strInt = Left(strInt, index - 1) & Mid(chnDigit, 5, 1) & Mid(strInt, index + 2)
- strInt = Left(strInt, index) & "零" & Mid(strInt, index + 1)
- Else
- strInt = Left(strInt, index - 1) & Mid(chnDigit, 5, 1) & Mid(strInt, index + 2)
- End If
- End If
- If Not bToRMB Then
- If Len(strInt) > 1 And Left(strInt, 2) = "一十" Then strInt = Mid(strInt, 2)
- End If
- ConvertIntegral = strInt
- End Function
-
- Private Function ConvertFractional(ByVal strFractional As String, ByVal bToRMB As Boolean) As String
- Dim i As Integer, mylen As Integer, index As Integer
- Dim strFrac As String
- mylen = Len(strFractional)
- If bToRMB Then
- For i = 1 To mylen
- index = Val(Mid(strFractional, i, 1)) + 1
- strFrac = strFrac & Mid(ChnRMBText, index, 1)
- strFrac = strFrac & Mid(ChnRMBUnit, i, 1)
- Next
- If Mid(strFrac, Len(strFrac) - 1, 2) = "零分" Then strFrac = Left(strFrac, Len(strFrac) - 2)
- If Left(strFrac, 2) = "零角" Then
- If Len(strFrac) = 2 Then
- strFrac = Mid(strFrac, 3)
- Else
- strFrac = Replace(strFrac, "角", "")
- End If
- End If
- Else
- For i = 1 To mylen
- index = Val(Mid(strFractional, i, 1)) + 1
- strFrac = strFrac & Mid(ChnGenText, index, 1)
- Next
- End If
- ConvertFractional = strFrac
- End Function
- Sub test()
- MsgBox ConvertChn("700010009.995", True)
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|