VB程序设计:将阿拉伯数字转换成汉字数字

2022-08-05 科技 166阅读
给你一个现成的,加两个控件,一个Label1,一个Text1:

Private Sub Form_Load()
'初始化设置
Text1.MaxLength = 16
Text1.Text = ""
Label1.Caption = ""
Label1.AutoSize = True
Label1.BorderStyle = 1
Label1.FontSize = 18
End Sub

Private Sub Text1_Change()
'调用转换子过程
Label1.Caption = CChinese(Text1.Text)
End Sub

Private Function CChinese(StrEng As String) As String
'验证数据
If Not IsNumeric(StrEng) Then
If Trim(StrEng) <> "" Then MsgBox "无效的数字"
CChinese = ""
Exit Function
End If
'定义变量
Dim intLen As Integer, intCounter As Integer
Dim strCh As String, strTempCh As String
Dim strSeqCh1 As String, strSeqCh2 As String
Dim strEng2Ch As String
strEng2Ch = "零壹贰叁肆伍陆柒捌玖"
strSeqCh1 = " 拾佰仟 拾佰仟 拾佰仟 拾佰仟"
strSeqCh2 = " 万亿兆"
'转换为表示数值的字符串
StrEng = CStr(CDec(StrEng))
'记录数字的长度
intLen = Len(StrEng)
'转换为汉字
For intCounter = 1 To intLen
'返回数字对应的汉字
strTempCh = Mid(strEng2Ch, Mid(StrEng, intCounter, 1) + 1, 1)
'若某位是零
If strTempCh = "零" And intLen <> 1 Then
'若后一个也是零,或零出现在倒数第1、5、9、13等位,则不显示汉字“零”
If Mid(StrEng, intCounter + 1, 1) = "0" Or (intLen - intCounter + 1) Mod 4 = 1 Then strTempCh = ""
Else
strTempCh = strTempCh & Trim(Mid(strSeqCh1, intLen - intCounter + 1, 1))
End If
'对于出现在倒数第1、5、9、13等位的数字
If (intLen - intCounter + 1) Mod 4 = 1 Then
'添加位" 万亿兆"
strTempCh = strTempCh & Trim(Mid(strSeqCh2, (intLen - intCounter) \ 4 + 1, 1))
End If
'组成汉字表达式
strCh = strCh & Trim(strTempCh)
Next
CChinese = strCh
End Function
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com