小写金额转换为大写金额(vb2010)

    '-------------------------------------------------------------
    '功能:小写金额转换为大写金额

    '参数:

    '返回:大写金额
    '     "0":整数位数超过9位
    '     "1":小数位数超过2位
    '     "零":num为0

    '方法:text1.text=numtost(213123.03)
    '-------------------------------------------------------------
    Public Function numToStr(ByVal Num As Double) As String
        Const strUnit = "元拾佰仟万拾佰仟亿拾佰仟"
        Const strDigit = "零壹贰叁肆伍陆柒捌玖"

        Dim strNum As String, strNumLeft As String, strNumRight As String
        Dim i As Integer, iCount As Integer
        Dim strFsbz As String   '负数标志

        '负数处理
        If Left(Str(Num), 1) = "-" Then
            strFsbz = "负"
            Num = Mid(Str(Num), 2)
        Else
            strFsbz = ""
        End If
        '数值转换为字符串及错误处理
        If Num = 0 Then
            numToStr = "零"
            Exit Function
        End If
        strNum = CStr(Num)
        If InStr(strNum, "E") <> 0 Then
            numToStr = "0"
            Exit Function
        End If
        If InStr(strNum, ".") <> 0 Then
            strNumLeft = Left(strNum, InStr(strNum, ".") - 1)
            strNumRight = Right(strNum, Len(strNum) - InStr(strNum, "."))
            '小数位数后只取2位
            strNumRight = Left(strNumRight, 2)
        Else
            strNumLeft = strNum
            strNumRight = ""
        End If
        If Len(strNumLeft) > 9 Then
            numToStr = "0"
            Exit Function
        End If
        If Len(strNumRight) > 2 Then
            numToStr = "1"
            Exit Function
        End If

        '转换大写金额
        '转大写数码
        For i = 0 To 9
            strNumLeft = Replace(strNumLeft, i, Mid(strDigit, i + 1, 1))
            strNumRight = Replace(strNumRight, i, Mid(strDigit, i + 1, 1))
        Next i

        '转换整数部分
        '插入数位字符
        iCount = Len(strNumLeft)
        For i = 0 To iCount - 1
            strNumLeft = Left(strNumLeft, iCount - i) & Mid(strUnit, i + 1, 1) & Right(strNumLeft, Len(strNumLeft) - iCount + i)
        Next i

        '(零仟、零佰、零拾变零)(零亿变亿,零万变万,零元变元)(零零变零)(亿万变亿)
        For i = 2 To 4
            While InStr(strNumLeft, "零" & Mid(strUnit, i, 1)) <> 0
                strNumLeft = Replace(strNumLeft, "零" & Mid(strUnit, i, 1), "零")
            End While
        Next i
        For i = 0 To 2
            While InStr(strNumLeft, "零" & Mid(strUnit, i * 4 + 1, 1)) <> 0
                strNumLeft = Replace(strNumLeft, "零" & Mid(strUnit, i * 4 + 1, 1), Mid(strUnit, i * 4 + 1, 1))
            End While
        Next i
        While InStr(strNumLeft, "零零") <> 0
            strNumLeft = Replace(strNumLeft, "零零", "零")
        End While
        strNumLeft = Replace(strNumLeft, "亿万", "亿")

        '转换小数部分
        Select Case Len(strNumRight)
            Case 0
                strNumRight = "整"
            Case 1
                strNumRight = strNumRight & "角整"
            Case 2
                strNumRight = Left(strNumRight, 1) & "角" & Right(strNumRight, 1) & "分整"
        End Select

        strNumRight = Replace(strNumRight, "零角", "零")
        strNumRight = Replace(strNumRight, "零分", "")
        strNumRight = Replace(strNumRight, "零整", "整")
        strNumRight = Replace(strNumRight, "分整", "分")

        numToStr = strNumLeft & strNumRight
        If Left(numToStr, 1) = "零" Then numToStr = Right(numToStr, Len(numToStr) - 1)
        If Left(numToStr, 1) = "元" Then numToStr = Right(numToStr, Len(numToStr) - 1)
        numToStr = strFsbz + numToStr
    End Function

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值