当前位置: 首页 > 图文教程 > .Net技术 > VB.NET > 实现将数字转换为汉字大写

VB.NET
[转]全面剖析VB.NET技术(1)
[转]全面剖析VB.NET技术(2)
基于VB.NET技术的表达式计算器
VB.NET多线程技术及其实现
走近VB.Net VB.Net问答全集
如何应用VB.NET MonthCalendar控件
VB.NET 拖动无边框窗体编程实例
.Net 虚拟框架的实现原理
实现将数字转换为汉字大写
vb.net access xml file
vb.net入门:MDI 窗体的基础使用
C#、VB.NET使用Windows API控制系统音量及静音
vb.net access xml file
VB.NET的阳历与农历转换的算法
用VB创建FTP组件(get)
vb中利用xmlhttp来下载远程文件
.net中快捷键定义
.NET对PE结构的扩展
VB.net中Delegate和Event
VB.NET DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串

VB.NET 中的 实现将数字转换为汉字大写


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 85 ::
收藏到网摘: n/a

在VB.net中如何实现将数字转换为汉字大写的人民币格式或数字格式。
'decimaltocn    [模块名:modDec2cn]
'制作:COSCO
'Email: [email protected]
'CopyRight(R): OceanStudio GuangZhou,China          2003年7月 
'------------------------------------------------------------------
'Imports System.Math

Module modDec2cn
    Public Function mf_dec2cn(ByVal number As Decimal, ByVal cntype As Integer) As String

        '--参数说明----------------------------------------------------
        '输入数字类型变量:number
        '返回字符串:  cnstr
        '参数整型变量:cntype: =0时,转换为人民币,=1时,转换为数字
        '<返回数字不大于18位精确值>
        '如:输入 1999.23  
        '转换为人民币:壹仟玖佰玖拾玖元贰角叁分
        '转换为数字为:壹仟玖佰玖拾玖点贰叁
        'cnstr=mf_dec2cn(number,cntype)
        '-------------------------------------------------------------
        Dim numstr() As String = _{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"}
        '定义取整数部分,小数部分,大写整数部分,大写小数部分的字符串变量
        Dim str1, str2, intstr, decstr As String
        Dim num() As String     '各个数位段,即每四位为一段
        Dim n() As String       '各位数字
        Dim number1, number2, number3 As String     '定义数字的三个数位段
        Dim pointpos As Integer
        ReDim num(3)
        ReDim n(7)

        If cntype = 0 Then
            '如果转换为人民币大写,小数位数只取2位
            number = Math.Round(number, 2)  'Round函数位于 System.Math 命名空间
            If IsDBNull(number) Then
                MessageBox.Show("转换为人民币大写的数字整数位数最大17位!", "错误", _
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Return ""
            End If
        End If
        pointpos = InStr(Str(number), ".")
        If pointpos = 0 Then    '输入值为纯整数
            str1 = Str(number)
            str2 = ""
        Else
            '输入值为浮点数
            str1 = Left(Str(number), pointpos - 1) '取整数部分
            str2 = Right(Str(number), Len(Str(number)) - pointpos) '取小数部分
        End If
        'If CDec(str2) = 0 Then '如果小数部分为0,则取消小数部分的分析
        '    str2 = ""
        'End If
     
        Select Case Len(str1)
            Case Is > 8 '分析整数部分在100000000以上的
                num(0) = Left(str1, Len(str1) - 8) '按每4位为一段拆分成三段,逐段分析
                num(1) = Left(Right(str1, 8), 4)
                num(2) = Right(str1, 4)

                number1 = mf_dec2cn(CDec(num(1)), 1) + "亿" '若干亿

                If Mid(num(1), 1, 1) = "0" Then '分析千万位
                    n(0) = numstr(1) '若该位为0,则数位赋值“零”
                Else
                    n(0) = numstr(Int(Mid(num(1), 1, 1)) + 1) + "仟"
                End If
                If Mid(num(1), 2, 1) = "0" Then '分析百万位
                    If Mid(num(1), 1, 1) = "0" Then
                        n(1) = "" '若千万位为0,则该位为空
                    Else
                        n(1) = numstr(1)
                    End If
                Else
                    n(1) = numstr(Int(Mid(num(1), 2, 1)) + 1) + "佰"
                End If
                If Mid(num(1), 3, 1) = "0" Then '分析十万位
                    If Mid(num(1), 2, 1) = "0" Then
                        n(2) = ""
                    Else
                        n(2) = numstr(1)
                    End If
                Else
                    n(2) = numstr(Int(Mid(num(1), 3, 1)) + 1) + "拾"
                End If
                If Mid(num(1), 4, 1) = "0" Then '分析万位
                    n(3) = ""
                Else
                    n(3) = numstr(Int(Mid(num(1), 4, 1)) + 1)
                End If

                If Right(num(1), 3) = "000" Then '当末尾有000时
                    n(1) = ""
                    n(2) = ""
                    n(3) = ""
                End If
                If Right(num(1), 2) = "00" Then '当末尾有00时
                    n(2) = ""
                    n(3) = ""
                End If
                If Right(num(1), 1) = "0" Then '当末尾有0时
                    n(3) = ""
                End If

                If Mid(num(2), 1, 1) = "0" Then '分析千位
                    n(4) = numstr(1)
                Else
                    n(4) = numstr(Int(Mid(num(2), 1, 1)) + 1) + "仟"
                End If
                If Mid(num(2), 2, 1) = "0" Then '分析百位
                    If Mid(num(2), 1, 1) = "0" Then
                        n(5) = ""
                    Else
                        n(5) = numstr(1)
                    End If
                Else
                    n(5) = numstr(Int(Mid(num(2), 2, 1)) + 1) + "佰"
                End If
                If Mid(num(2), 3, 1) = "0" Then '分析十位
                    If Mid(num(2), 2, 1) = "0" Then
                        n(6) = ""
                    Else
                        n(6) = numstr(1)
                    End If
                Else
                    n(6) = numstr(Int(Mid(num(2), 3, 1)) + 1) + "拾"
                End If
                If Mid(num(2), 4, 1) = "0" Then '分析个位
                    n(7) = ""
                Else
                    n(7) = numstr(Int(Mid(num(2), 4, 1)) + 1)
                End If
                '
                If Right(num(2), 3) = "000" Then '当末尾有000时
                    n(5) = ""
                    n(6) = ""
                    n(7) = ""
                End If
                If Right(num(2), 2) = "00" Then '当末尾有00时
                    n(6) = ""
                    n(7) = ""
                End If
                If Right(num(2), 1) = "0" Then '当末尾有0时
                    n(7) = ""
                End If
                '
                '统计
                number2 = n(0) + n(1) + n(2) + n(3) + "万"
                If num(2) = "0000" Then
                    number3 = ""
                    If num(1) = "0000" Then
                        number2 = ""
                    End If
                Else
                    If num(1) = "0000" Then
                        number2 = numstr(1)
                    End If
                    number3 = n(4) + n(5) + n(6) + n(7)
                End If
                '取得整数位值
                intstr = number1 + number2 + number3

            Case 5 To 8 '分析整数部分在10000~99999999之间的
                num(0) = Left(str1, Len(str1) - 4) '取得第一段(千万位到万位)
                '不足4位,用'0'补齐
                If Len(num(0)) = 3 Then
                    num(0) = "0" + num(0)
                End If
                If Len(num(0)) = 2 Then
                    num(0) = "00" + num(0)
                End If
                If Len(num(0)) = 1 Then
                    num(0) = "000" + num(0)
                End If
                num(1) = Right(str1, 4) '取得第二段(千位到个位)
                number1 = ""
                If Mid(num(0), 1, 1) = "0" Then '分析千万位
                    n(0) = ""
                Else
                    n(0) = numstr(CInt(Mid(num(0), 1, 1)) + 1) + "仟"
                End If
                If Mid(num(0), 2, 1) = "0" Then '分析百万位
                    If Mid(num(0), 1, 1) = "0" Then
                        n(1) = ""
                    Else
                        n(1) = numstr(1)
                    End If
                Else
                    n(1) = numstr(CInt(Mid(num(0), 2, 1)) + 1) + "佰"
                End If
                If Mid(num(0), 3, 1) = "0" Then '分析十万位
                    If Mid(num(0), 2, 1) = "0" Then
                        n(2) = ""
                    Else
                        n(2) = numstr(1)
                    End If
                Else
                    n(2) = numstr(CInt(Mid(num(0), 3, 1)) + 1) + "拾"
                End If
                If Mid(num(0), 4, 1) = "0" Then '分析万位
                    n(3) = ""
                Else
                    n(3) = numstr(CInt(Mid(num(0), 4, 1)) + 1)
                End If
                '
                If Right(num(0), 3) = "000" Then '当末尾有000时
                    n(1) = ""
                    n(2) = ""
                    n(3) = ""
                End If
                If Right(num(0), 2) = "00" Then '当末尾有00时
                    n(2) = ""
                    n(3) = ""
                End If
                If Right(num(0), 1) = "0" Then '当末尾有0时
                    n(3) = ""
                End If
                '
                If Mid(num(1), 1, 1) = "0" Then '分析千位
                    n(4) = numstr(1)
                Else
                    n(4) = numstr(CInt(Mid(num(1), 1, 1)) + 1) + "仟"
                End If
                If Mid(num(1), 2, 1) = "0" Then '分析百位
                    If Mid(num(1), 1, 1) = "0" Then
                        n(5) = ""
                    Else
                        n(5) = numstr(1)
                    End If
                Else
                    n(5) = numstr(CInt(Mid(num(1), 2, 1)) + 1) + "佰"
                End If
                If Mid(num(1), 3, 1) = "0" Then '分析十位
                    If Mid(num(1), 2, 1) = "0" Then
                        n(6) = ""
                    Else
                        n(6) = numstr(1)
                    End If
                Else
                    n(6) = numstr(CInt(Mid(num(1), 3, 1)) + 1) + "拾"
                End If
                If Mid(num(1), 4, 1) = "0" Then '分析个位
                    n(7) = ""
                Else
                    n(7) = numstr(CInt(Mid(num(1), 4, 1)) + 1)
                End If
                '
                If Right(num(1), 3) = "000" Then '当末尾有000时
                    n(5) = ""
                    n(6) = ""
                    n(7) = ""
                End If
                If Right(num(1), 2) = "00" Then '当末尾有00时
                    n(6) = ""
                    n(7) = ""
                End If
                If Right(num(1), 1) = "0" Then '当末尾有0时
                    n(7) = ""
                End If
                '
                '统计
                If num(1) = "0000" Then
                    number3 = ""
                Else
                    number3 = n(4) + n(5) + n(6) + n(7)
                End If
                number2 = n(0) + n(1) + n(2) + n(3) + "万"
                '取得整数位值
                intstr = number1 + number2 + number3
            Case Else   '分析整数部分不到10000的
                num(0) = str1
                '不足4位,用'0'补齐
                If Len(num(0)) = 3 Then
                    num(0) = "0" + num(0)
                End If
                If Len(num(0)) = 2 Then
                    num(0) = "00" + num(0)
                End If
                If Len(num(0)) = 1 Then
                    num(0) = "000" + num(0)
                End If
                number1 = ""
                number2 = ""
                If Mid(num(0), 1, 1) = "0" Then '分析千位
                    n(0) = ""
                Else
                    n(0) = numstr(CInt(Mid(num(0), 1, 1)) + 1) + "仟"
                End If
                If Mid(num(0), 2, 1) = "0" Then '分析百位
                    If Mid(num(0), 1, 1) = "0" Then
                        n(1) = ""
                    Else
                        n(1) = numstr(1)
                    End If
                Else
                    n(1) = numstr(CInt(Mid(num(0), 2, 1)) + 1) + "佰"
                End If
                If Mid(num(0), 3, 1) = "0" Then '分析十位
                    If Mid(num(0), 2, 1) = "0" Then
                        n(2) = ""
                    Else
                        n(2) = numstr(1)
                    End If
                Else
                    n(2) = numstr(CInt(Mid(num(0), 3, 1))) + "拾" '
                End If
                If Mid(num(0), 4, 1) = "0" Then '分析个位
                    n(3) = ""
                Else
                    n(3) = numstr(CInt(Mid(num(0), 4, 1)) + 1)
                End If
                '
                If Right(num(0), 3) = "000" Then '当末尾有000时
                    n(1) = ""
                    n(2) = ""
                    n(3) = ""
                End If
                If Right(num(0), 2) = "00" Then '当末尾有00时
                    n(2) = ""
                    n(3) = ""
                End If
                If Right(num(0), 1) = "0" Then '当末尾有0时
                    n(3) = ""
                End If
                '
                '统计
                number3 = n(0) + n(1) + n(2) + n(3)
                ''取得整数位值
                intstr = number1 + number2 + number3
        End Select

        If str1 = "0" Then '如果整数为零,转换为"零"
            intstr = numstr(1)
        End If

        '==============分析和转换小数部分==============
        If cntype = 0 Then '如果转换为人民币表达式
            '   if len(str2)>2 then'如果小数位数大于2,就四舍五入.
            '      str2=right(string(round(double("0"+"."+str2),2)),2)
            '   end if
            If Mid(str2, 1, 1) = "0" Then
                n(0) = "零"
            Else
                n(0) = numstr(CInt(Mid(str2, 1, 1)) + 1) + "角"
            End If
            If Mid(str2, 2, 1) = "0" Then
                n(1) = ""
            Else
                n(1) = numstr(CInt(Mid(str2, 2, 1)) + 1) + "分"
            End If
            decstr = n(0) + n(1)
        End If
        If cntype = 1 Then '如果转换为数字表达式
            Dim decpos As Integer
            For decpos = 1 To Len(str2)
                n(decpos) = numstr(CInt(Mid(str2, decpos, 1)) + 1)
                decstr = decstr + n(decpos)
            Next
        End If

        '================输出本函数的结果================

        If cntype = 0 Then '将数字字串转换为人民币的大写格式
            If str2 = "" Then '如果为纯整数
                Return intstr + "元整"
            Else
                If intstr = "零" Then '如果整数为零,就只显示小数
                    Return decstr
                Else
                    Return intstr + "元" + decstr
                End If
            End If
        End If
        If cntype = 1 Then '将数字字串转换为普通大写格式
            If str2 = "" Then '如果为纯整数
                Return intstr
            Else
                Return intstr + "点" + decstr
            End If
        End If
    End Function
End Module