Office中国论坛/Access中国论坛

标题: [分享]人民币小写转换大写 [打印本页]

作者: 3332203    时间: 2007-3-17 01:23
标题: [分享]人民币小写转换大写
       这几天在这里学到不少东西,所以也想给大家分享点。[attach]23579[/attach]


[此贴子已经被作者于2007-3-16 17:28:53编辑过]


作者: fan0217    时间: 2007-3-17 01:29
给你些建议:

1.可以写成函数的形式

2.代码太整齐了,相反不太好认,最好有合理的缩进。

3.支持转换的数字太小,上亿就会出现错误。

4.对于一个程序而言,错误处理是必要的。

5.前缀不一定都需要,可以设置成参数,让用户自己定义。



[此贴子已经被作者于2007-3-16 17:42:03编辑过]


作者: fan0217    时间: 2007-3-17 02:38
给你修改成函数了:

'***********************************代码开始***********************************
Public Function CMoneyToCnCap(Money As Double) As String
On Error GoTo CMoneyToCnCap_Err
Dim strTemp As String
Dim intDigit As Integer
Dim strCnTempDigit As String
Dim strCnTemp As String
Dim intLen As Integer
Dim i As Integer
Dim strCnTempNum As String
Dim strDeno As String

    If Money > (1 * 10 ^ 13) Or Money < (-1 * 10 ^ 13) Then
        MsgBox "输入的数值的整数位数不能超过 13 位。", vbOKOnly
        Exit Function
    End If
   
    strCnTempNum = "零壹贰叁肆伍陆柒捌玖拾"
    strDeno = "分角元拾佰仟万拾佰仟亿拾佰仟万拾佰"
    strTemp = CStr(Abs(Round(Money, 2) * 100))
    intLen = Len(strTemp)
  
    If Money = 0 Then
        CMoneyToCnCap = "零元整"
        Exit Function
    End If
   
    For i = intLen To 1 Step -1
        intDigit = Mid(strTemp, intLen - i + 1, 1)
        strCnTempDigit = Mid(strCnTempNum, intDigit + 1, 1)
        strCnTemp = strCnTemp & strCnTempDigit & Mid(strDeno, i, 1)
    Next
   
    strCnTemp = Replace(strCnTemp, "零拾", "零")
    strCnTemp = Replace(strCnTemp, "零佰", "零")
    strCnTemp = Replace(strCnTemp, "零仟", "零")
    strCnTemp = Replace(strCnTemp, "零零零", "零")
    strCnTemp = Replace(strCnTemp, "零零", "零")
    strCnTemp = Replace(strCnTemp, "零角零分", "整")
    strCnTemp = Replace(strCnTemp, "零分", "整")
    strCnTemp = Replace(strCnTemp, "零角", "零")
    strCnTemp = Replace(strCnTemp, "零亿零万", "亿")
    strCnTemp = Replace(strCnTemp, "零亿零万零元", "亿元")
    strCnTemp = Replace(strCnTemp, "亿零万零元", "亿元")
    strCnTemp = Replace(strCnTemp, "零亿零万", "亿")
    strCnTemp = Replace(strCnTemp, "零万零元", "万元")
    strCnTemp = Replace(strCnTemp, "万零元", "万元")
    strCnTemp = Replace(strCnTemp, "零亿", "亿")
    strCnTemp = Replace(strCnTemp, "零万", "万")
    strCnTemp = Replace(strCnTemp, "零元", "元")
    strCnTemp = Replace(strCnTemp, "零零", "零")

    If Money > 0 Then
        CMoneyToCnCap = strCnTemp
    Else
        CMoneyToCnCap = "负" & strCnTemp
    End If
     
CMoneyToCnCap_Exit:
    Exit Function
   
CMoneyToCnCap_Err:
    MsgBox Err.Description
    Resume CMoneyToCnCap_Exit
   
End Function
'***********************************代码结束***********************************

作者: 3332203    时间: 2007-3-17 03:03
    多谢版主指点!
作者: nxjswt    时间: 2007-3-17 18:58
学习斑竹致电!
作者: fan0217    时间: 2007-3-18 03:16
论坛上有不少这样的函数,但都有点问题,大家可以试试,如果数字比较大,中间的零比较多,那就出问题了。




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3