阿杰 发表于 2010-8-8 14:54:16

将阿拉伯数字转成中文字

一个Form
               一个TextBox
               一个Label

这个修订後的程式是当使用者在TextBox中输入只包含 0~9 的数值後,在Label中就可
看见被转换後的中文字,例如:1560890 转成 "壹佰伍拾陆万零捌佰玖拾"。程式限制
为不可输入超过16个数字。

请建立一个新专案,并在表单中放入上述物件,再把以下程式码复制到表单的程式
码视窗,最後按下F5来执行。

Private Sub Form_Load()
       Text1.MaxLength = 16
       Text1.Text = ""
       Label1.Caption = ""
       Label1.AutoSize = True
       Label1.BorderStyle = 1
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) Or StrEng Like "*.*" Or StrEng Like "*-*" 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, Val(Mid(StrEng, intCounter, 1)) + 1, 1)
            If strTempCh = "零" And intLen <> 1 Then
               If Mid(StrEng, intCounter + 1, 1) = "0" Or (intLen - intCounter + 1) Mod 4 = 1 Then
                      strTempCh = ""
               End If
            Else
               strTempCh = strTempCh & Trim(Mid(strSeqCh1, intLen - intCounter + 1, 1))
            End If
            If (intLen - intCounter + 1) Mod 4 = 1 Then
               strTempCh = strTempCh & Mid(strSeqCh2, (intLen - intCounter + 1) \ 4 + 1, 1)
               If intCounter > 3 Then
                      If Mid(StrEng, intCounter - 3, 4) = "0000" Then strTempCh = Left(strTempCh, Len(strTempCh) - 1)
                End If
            End If
            strCh = strCh & Trim(strTempCh)
       Next
       CChinese = strCh
End Function
页: [1]
查看完整版本: 将阿拉伯数字转成中文字