本人在网上下载一段关于货币小写转大写的代码,测试发现在计算到“分”时有不准确现象,如将“12345.38”换算为“壹万贰仟叁佰肆拾伍元叁角柒分”。请大家帮忙看一下。代码如下:
Function SubtoChinese(price As Integer)
    Dim CHAp(21, 1)
    CHAp(0, 0) = "万": CHAp(0, 1) = 10000
    CHAp(1, 0) = "仟": CHAp(1, 1) = 1000
    CHAp(2, 0) = "佰": CHAp(2, 1) = 100
    CHAp(3, 0) = "拾": CHAp(3, 1) = 10
    CHAp(4, 0) = "元": CHAp(4, 1) = 1
    CHAp(5, 0) = "角": CHAp(5, 1) = 0.1
    CHAp(6, 0) = "分": CHAp(6, 1) = 0.01
    CHAp(11, 0) = "壹": CHAp(11, 1) = 1
    CHAp(12, 0) = "贰": CHAp(12, 1) = 2
    CHAp(13, 0) = "叁": CHAp(13, 1) = 3
    CHAp(14, 0) = "肆": CHAp(14, 1) = 4
    CHAp(15, 0) = "伍": CHAp(15, 1) = 5
    CHAp(16, 0) = "陆": CHAp(16, 1) = 6
    CHAp(17, 0) = "柒": CHAp(17, 1) = 7
    CHAp(18, 0) = "捌": CHAp(18, 1) = 8
    CHAp(19, 0) = "玖": CHAp(19, 1) = 9
    CHAp(20, 0) = "零": CHAp(20, 1) = 0
    CHAp(21, 0) = "亿": CHAp(21, 1) = 100000000
'转化千百十
Dim I As Integer
Dim num(15) As Integer
I = 1
    Do Until price = 0
        num(I) = Int(price / CHAp(I, 1))
        If num(I) <> 0 Then
            SubtoChinese = SubtoChinese & CHAp(num(I) + 10, 0) & CHAp(I, 0)
            price = price - num(I) * CHAp(I, 1)
        Else
            If SubtoChinese <> "" And Right(SubtoChinese, 1) <> "零" Then
                SubtoChinese = SubtoChinese & "零"
            End If
        End If
        I = I + 1
    Loop
    If Right(SubtoChinese, 1) = "元" Then
        SubtoChinese = Left(SubtoChinese, Len(SubtoChinese) - 1)
    End If
End FunctionFunction PricetoChinese(price As Double)
    Dim CHAp(21, 1)
    CHAp(0, 0) = "万": CHAp(0, 1) = 10000
    CHAp(1, 0) = "仟": CHAp(1, 1) = 1000
    CHAp(2, 0) = "佰": CHAp(2, 1) = 100
    CHAp(3, 0) = "拾": CHAp(3, 1) = 10
    CHAp(4, 0) = "元": CHAp(4, 1) = 1
    CHAp(5, 0) = "角": CHAp(5, 1) = 0.1
    CHAp(6, 0) = "分": CHAp(6, 1) = 0.01
    CHAp(11, 0) = "壹": CHAp(11, 1) = 1
    CHAp(12, 0) = "贰": CHAp(12, 1) = 2
    CHAp(13, 0) = "叁": CHAp(13, 1) = 3
    CHAp(14, 0) = "肆": CHAp(14, 1) = 4
    CHAp(15, 0) = "伍": CHAp(15, 1) = 5
    CHAp(16, 0) = "陆": CHAp(16, 1) = 6
    CHAp(17, 0) = "柒": CHAp(17, 1) = 7
    CHAp(18, 0) = "捌": CHAp(18, 1) = 8
    CHAp(19, 0) = "玖": CHAp(19, 1) = 9
    CHAp(20, 0) = "零": CHAp(20, 1) = 0
    CHAp(21, 0) = "亿": CHAp(21, 1) = 100000000    If price >= 100000000 Then   '大于1亿
        PricetoChinese = PricetoChinese & PricetoChinese(Int(price / 100000000)) & "亿"
        price = price - Int(price / 100000000) * 100000000
    End If
    If price >= 10000 Then
        PricetoChinese = PricetoChinese & SubtoChinese(Int(price / 10000)) & "万"
        price = price - Int(price / 10000) * 10000
    End If
    If Int(price) <> 0 Then '如果万与千间无数,则应添零
        If PricetoChinese <> "" And Int(price) < 1000 Then
            PricetoChinese = PricetoChinese & "零"
        End If
        PricetoChinese = PricetoChinese & SubtoChinese(Int(price))
        price = price - Int(price)
    End If
    If PricetoChinese <> "" Then PricetoChinese = PricetoChinese & "元"
    If price = 0 Then '到元为止
        PricetoChinese = PricetoChinese & "整"
    Else
        price = Int(price * 100)
        If Int(price / 10) <> 0 Then
            PricetoChinese = PricetoChinese & CHAp(Int(price / 10) + 10, 0) & "角"
            price = price - Int(price / 10) * 10
         End If
         If price <> 0 Then
            PricetoChinese = PricetoChinese & CHAp(Int(price) + 10, 0) & "分"
            
         End If
    End If
End FunctionPrivate Sub Command1_Click()
    Text1.Text = PricetoChinese(12345.38)
End Sub

解决方案 »

  1.   

    懒得看了,给你一个函数把
    '**************************************************************************
    '函数用途:将数字串转换为中文金额字符串
    '入口参数:Digital 要转换的数字串
    '返回参数:中文金额字符串
    '限制:Digital小于1亿
    '*************************************************************************
    Public Function ConvToMoney(ByVal Digital As String) As String
    Dim strChi(11), strDig(10)  As String
    Dim stmp, rsStr As String
    Dim lenStr As Byte
    Dim laststr As StringIf Not IsNumeric(Digital) Then
        ConvToMoney = ""
        Exit Function
    End IfIf Val(Format(Digital)) < 0 Then
        Digital = Trim(Str(Abs(Val(Format(Digital)))))
        laststr = "整(负)"
    Else
        laststr = "整"
    End IfstrChi(0) = "分"
    strChi(1) = "角"
    strChi(2) = "元"
    strChi(3) = "拾"
    strChi(4) = "佰"
    strChi(5) = "仟"
    strChi(6) = "万"
    strChi(7) = "拾"
    strChi(8) = "佰"
    strChi(9) = "仟"
    strChi(10) = "亿"
    strDig(0) = "零"
    strDig(1) = "壹"
    strDig(2) = "贰"
    strDig(3) = "叁"
    strDig(4) = "肆"
    strDig(5) = "伍"
    strDig(6) = "陆"
    strDig(7) = "柒"
    strDig(8) = "捌"
    strDig(9) = "玖"
    stmp = Digital
    If (Len(stmp) = 0) Or (Len(stmp) > 11) Then
     ConvToMoney = ""
     Exit Function
    End If
    stmp = Format(stmp, "########.00")
    If Len(stmp) > 11 Then
     ConvToMoney = ""
     Exit Function
    End IflenStr = Len(stmp)
     rsStr = strDig(Val(Mid(stmp, lenStr - 1, 1))) & strChi(1) & strDig(Val(Right(stmp, 1))) & strChi(0)
     stmp = Left(stmp, Len(stmp) - 3)
    Dim I, d As Byte
    Dim blnZero As Boolean
    Dim stmprv, dstr As StringFor I = 1 To Len(stmp)
      stmprv = Mid(stmp, I, 1) & stmprv
    NextFor I = 1 To Len(stmprv)
     d = Val(Mid(stmprv, I, 1))
     If d = 0 Then
       If I = 1 Or I = 5 Then
          dstr = strChi(I + 1)
       Else
         If Not blnZero Then
          dstr = strDig(0)
         Else
          dstr = ""
         End If
       End If
       blnZero = True
     Else
       dstr = strDig(d) & strChi(I + 1)
       blnZero = False
     End If
     rsStr = dstr + rsStr
    Next
    ConvToMoney = rsStr & laststr
    End Function
      

  2.   

    '****************************************************************************************************
    '功能:金额小写转大写
    '日期:2005-04-20
    '参数:
    '   strNum  数字金额
    '返回:大写金额
    '****************************************************************************************************
    Private Function NumToChar(ByVal strNum As String) As String
        Dim String1 As String, String2 As String, String3 As String
        Dim Ch1 As String, Ch2 As String, Atoc As String
        Dim lngTmp As Double, nZero As Long, lngJ As Long, lngI As Long
        
        String1 = "零壹贰叁肆伍陆柒捌玖"
        String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"
        lngTmp = Val(strNum)
        
        nZero = 0    lngJ = Len(CStr(lngTmp * 100))
        String2 = Right(String2, lngJ) '取出对应位数的STRING2的值    For lngI = 1 To lngJ
            String3 = Mid(lngTmp * 100, lngI, 1) '取出需转换的某一位的值        If lngI <> (lngJ - 3) + 1 And lngI <> (lngJ - 7) + 1 And lngI <> (lngJ - 11) + 1 And lngI <> (lngJ - 15) + 1 Then
                If String3 = 0 Then
                    Ch1 = ""
                    Ch2 = ""
                    nZero = nZero + 1
                ElseIf String3 <> 0 And nZero <> 0 Then
                    Ch1 = "零" & Mid(String1, CLng(String3) + 1, 1)
                    Ch2 = Mid(String2, lngI, 1)
                    nZero = 0
                Else
                    Ch1 = Mid(String1, CLng(String3) + 1, 1)
                    Ch2 = Mid(String2, lngI, 1)
                    nZero = 0
                End If
            Else '该位是万亿,亿,万,元位等关键位
                If String3 <> 0 And nZero <> 0 Then
                    Ch1 = "零" & Mid(String1, CLng(String3) + 1, 1)
                    Ch2 = Mid(String2, lngI, 1)
                    nZero = 0
                ElseIf String3 <> 0 And nZero = 0 Then
                    Ch1 = Mid(String1, CLng(String3) + 1, 1)
                    Ch2 = Mid(String2, lngI, 1)
                    nZero = 0
                ElseIf String3 = 0 And nZero >= 3 Then
                    Ch1 = ""
                    Ch2 = ""
                    nZero = nZero + 1
                Else
                    Ch1 = ""
                    Ch2 = Mid(String2, lngI, 1)
                    If lngI <> Len(CStr(lngTmp * 100)) - 2 Then nZero = nZero + 1
                End If            If lngI = (lngJ - 11) + 1 Or lngI = (lngJ - 3) + 1 Then '如果该位是亿位或元位,则必须写上
                    Ch2 = Mid(String2, lngI, 1)
                End If        End If
            Atoc = Atoc & Ch1 & Ch2        If lngI = lngJ And Right(lngTmp * 100, 2) = 0 Then '最后一位(分)为0时,加上“整”
                Atoc = Atoc & "整"
            End If    Next
        
        If lngTmp = 0 Then
            Atoc = "零元整"
        End If
        NumToChar = Atoc
    End Function