VB6.0小写金额转大写金额
因票据打印的需要,在网上搜罗许久,到的代码均不如意,于是干脆自己编写一个,并发布出来,也许对他人有点帮助。
一、控件:一个TextBox,三个CommandButten(转换、清屏、退出),一个ListBox
二、说明:之所以用ListBox来存储结果,是为了便于校验转换对错,用户根据需要改变。本程序不能处理位数大于千亿的数字。
三、代码:
Private Sub Command1_Click()
'转换
    If Text1.Text <> "" Then
        List1.AddItem Text1.Text & vbTab & NumberToCharacter(Text1.Text)
    End If
    Text1.Text = ""
    Text1.SetFocus
End Sub
Private Sub Command2_Click()
'清屏
    Text1.Text = ""
    List1.Clear
End Sub
Private Sub Command3_Click()
'退出
    Unload Me
End Sub
Public Function NumberToCharacter(NumBer As String)
    If NumBer = "" Then Exit Function
   
    Dim Pos_Point As Long '记录小数点的位置
    Dim NumToStr As String  '用于记录转换后的结果
Dim oldXX, nowXX As String  '用于提取上一位数的值、当前位数的值
NumToStr = ""
   
    '预处理
    If InStr(NumBer, ",") > 0 Then NumBer = Replace(NumBer, ",", "")
    If InStr(NumBer, ",") > 0 Then NumBer = Replace(NumBer, ",", "")
    Pos_Point = InStr(NumBer, ".")
    If Pos_Point = 1 Then
        NumBer = "0" & NumBer
        Pos_Point = Pos_Point + 1
    Else
        If Pos_Point > 2 Then    '去除无效的0
            For i = 1 To Pos_Point - 2
                If Mid(NumBer, 1, 1) = "0" Then
                    NumBer = Mid(NumBer, 2, Len(NumBer) - 1)
                    Pos_Point = Pos_Point - 1
                End If
            Next i
        End If
    End If
   
    If Pos_Point = 0 Then
        If Len(NumBer) > 12 Then MsgBox "不能处理位数大于【千亿】的数字!", vbYes, "警告:": GoTo js
    Else
        If Len(Mid(NumBer, 1, Pos_Point - 1)) > 12 Then MsgBox "不能处理位数大于【千亿】
的数字!", vbYes, "警告:": GoTo js
    End If
   
    '处理小数部分
    If Pos_Point = 0 Then
        NumToStr = "整"
    Else
        Select Case Len(NumBer) - Pos_Point
            Case Is >= 2
                If Mid(NumBer, Pos_Point + 2, 1) = "0" Then  '如果分等于零
                    If Mid(NumBer, Pos_Point + 1, 1) = "0" Then    '如果角等于零
                        NumToStr = "整"
                    Else
                        NumToStr = CCh(Mid(NumBer, Pos_Point + 1, 1)) & "角零分"
                    End If
                Else
                    NumToStr = CCh(Mid(NumBer, Pos_Point + 2, 1)) & "分"
                    NumToStr = CCh(Mid(NumBer, Pos_Point + 1, 1)) & "角" & NumToStr
                End If
            Case 1
字符串函数中将大写转换为小写                If Right(NumBer, 1) = "0" Then
                    NumToStr = "整"
                Else
                    NumToStr = CCh(Mid(NumBer, Pos_Point + 1, 1)) & "角零分"
                End If
            Case 0
                NumToStr = "整"
        End Select
        NumBer = Mid(NumBer, 1, Pos_Point - 1)
    End If
   
   
    '个位
    nowXX = Mid(NumBer, Len(NumBer), 1)
    If nowXX = "0" Then
        If NumToStr <> "整" Then
            If Len(NumBer) > 1 Then
                NumToStr = "元零" & NumToStr
            End If
        Else
            NumToStr = "元" & NumToStr
        End If
    Else
        NumToStr = CCh(Mid(NumBer, Len(NumBer), 1)) & "元" & NumToStr
    End If
    oldXX = nowXX
   
    '十位
    If Len(NumBer) < 2 Then GoTo scNumToStr
    nowXX = Mid(NumBer, Len(NumBer) - 1, 1)
    If nowXX = "0" Then
        If oldXX <> "0" Then NumToStr = "零" & NumToStr
    Else
        NumToStr = CCh(Mid(NumBer, Len(NumBer) - 1, 1)) & "拾" & NumToStr
    End If
    oldXX = nowXX
   
    '百位
    If Len(NumBer) < 3 Then GoTo scNumToStr
    nowXX = Mid(NumBer, Len(NumBer) - 2, 1)
    If nowXX = "0" Then
        If oldXX <> "0" Then NumToStr = "零" & NumToStr          ' And Mid(NumToStr, 1, 2) <> "元零"
    Else
        NumToStr = CCh(Mid(NumBer, Len(NumBer) - 2, 1)) & "佰" & NumToStr
    End If
    oldXX = nowXX
   
    '千位
    If Len(NumBer) < 4 Then GoTo scNumToStr
    nowXX = Mid(NumBer, Len(NumBer) - 3, 1)
    If nowXX = "0" Then
        If oldXX <> "0" Then NumToStr = "零" & NumToStr
    Else
        NumToStr = CCh(Mid(NumBer, Len(NumBer) - 3, 1)) & "仟" & NumToStr
    End If
    oldXX = nowXX
   
    '万位
    If Len(NumBer) < 5 Then GoTo scNumToStr
    nowXX = Mid(NumBer, Len(NumBer) - 4, 1)
    If nowXX = "0" Then
        If oldXX <> "0" Then NumToStr = "零" & NumToStr
    Else
        NumToStr = CCh(Mid(NumBer, Len(NumBer) - 4, 1)) & "万" & NumToStr
    End If
    oldXX = nowXX
   
    '十万位
    If Len(NumBer) < 6 Then GoTo scNumToStr
    nowXX = Mid(NumBer, Len(NumBer) - 5, 1)
    If nowXX = "0" Then
        If oldXX <> "0" Then NumToStr = "零" & NumToStr

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。