人民币大写转化函数(VB.NET版)

看了二十四画生的Blog的大小写金额转换函数,自己也对照着写了个VB.NET的,然后用类封装了一下。
原版C#版的,可以看原作者的代码

    Public Class Currency

        Inherits Object

 

        Private Const CST_CAPSTR_TAIL As String = ""

        Private Const CST_CAPSSTR_ZERO As String = ""

        Private Const CST_CAPSSTR As String = CST_CAPSSTR_ZERO & "贰叁肆伍柒捌玖"

        Private Const CST_POSSTR_YUAN As String = ""

        Private Const CST_POSSTR As String = "万仟佰拾亿仟佰拾万仟佰拾" & CST_POSSTR_YUAN & "角分"

        Private Const CST_CHNNUM_ZERO As String = CST_CAPSSTR_ZERO & CST_POSSTR_YUAN & CST_CAPSTR_TAIL

 

        Private Shared ReadOnly CST_MAXLENGTH As Integer = CST_POSSTR.Length

 

        Public Shared Function ConvertToCapsString(ByVal dec As Decimal) As String

            Dim i, j, nZero As Integer

            Dim strDec, strPos, ch1, ch2 As String

            Dim chnNum, chnPos As String

            Dim strOnePosValue As String

            Dim intOnePosValue As Integer

 

            'num绝对值并四舍五入取2位小数

            dec = Math.Round(Math.Abs(dec), 2)

            'num100转换成字符串形式

            strDec = Convert.ToInt64(dec * 100).ToString

            '找出最高位

            j = strDec.Length

            If i > CST_MAXLENGTH Then

                Throw New OverflowException

            ElseIf dec = 0 Then

                '零元的候,直接出“零元整”

                chnNum = CST_CHNNUM_ZERO

            Else

                '取出对应位数的str2。如:200.55,j5所以str2=佰拾元角分

                strPos = CST_POSSTR.Substring(CST_MAXLENGTH - j)

                '取出一位需要转换

                For i = 0 To j - 1

                    '取出需转换的某一位的

                    strOnePosValue = strDec.Substring(i, 1)

                    '转换为数字

                    intOnePosValue = Convert.ToInt32(strOnePosValue)

                    If i <> j - 3 AndAlso i <> j - 7 AndAlso i <> j - 15 Then

                        '当所取位数不元、万、亿、万亿上的数字

                        If strOnePosValue = "0" Then

                            '此位零,只入零的个数。不转换

                            ch1 = ""

                            ch2 = ""

                            nZero = nZero + 1

                        Else

                            '此位非零

                            If nZero = 0 Then

                                   '前面没有零,追加此位的中文

                                   ch1 = CST_CAPSSTR.Substring(intOnePosValue * 1, 1)

                                   ch2 = strPos.Substring(i, 1)

                                   nZero = 0

                            Else

                                   '前面已有零,无有多少只加一个零,然后追加此位的中文

                                   ch1 = CST_CAPSSTR_ZERO & CST_CAPSSTR.Substring(intOnePosValue * 1, 1)

                                   ch2 = strPos.Substring(i, 1)

                                   nZero = 0

                            End If

                        End If

                    Else

                        '位是万亿亿,万,元位等关键

                        If strOnePosValue <> "0" AndAlso nZero <> 0 Then

                            ch1 = CST_CAPSSTR_ZERO & CST_CAPSSTR.Substring(intOnePosValue * 1, 1)

                            ch2 = strPos.Substring(i, 1)

                            nZero = 0

                        Else

                            If strOnePosValue <> "0" AndAlso nZero = 0 Then

                                   ch1 = CST_CAPSSTR.Substring(intOnePosValue * 1, 1)

                                   ch2 = strPos.Substring(i, 1)

                                   nZero = 0

                            Else

                                   If strOnePosValue = "0" AndAlso nZero >= 3 Then

                                       ch1 = ""

                                       ch2 = ""

                                       nZero = nZero + 1

                                   Else

                                       If j > 11 Then

                                           '亿以上

                                           ch1 = ""

                                           nZero = nZero + 1

                                       Else

                                           '亿以下

                                           ch1 = ""

                                           ch2 = strPos.Substring(i, 1)

                                           nZero = nZero + 1

                                       End If

                                   End If

                            End If

                        End If

                    End If

                    If i = j - 11 OrElse i = j - 3 Then

                        '如果位是亿位或元位,写上

                        ch2 = strPos.Substring(i, 1)

                    End If

                    chnNum = chnNum & ch1 & ch2

                    If i = j - 1 AndAlso strOnePosValue = "0" Then

                        '最后一位(分)0,加上

                        chnNum = chnNum & CST_CAPSTR_TAIL

                    End If

                Next

            End If

            Return chnNum

        End Function

 

        Public Shared Function ConvertToCapsString(ByVal dec As String) As String

            Dim num As Decimal = 0

            Try

                num = Convert.ToDecimal(dec)

            Catch ex As Exception

                Throw New InvalidateDecimalFormatException(dec)

            End Try

            Return ConvertToCapsString(num)

        End Function

 

        Public Class OverflowException

            Inherits Exception

 

            Public Sub New()

                MyBase.New("Overflow in parameter. To string length must less than " & CST_MAXLENGTH & ".")

            End Sub

 

        End Class

 

        Public Class InvalidateDecimalFormatException

            Inherits Exception

 

            Private m_val As String

 

            Public ReadOnly Property Value() As String

                Get

                    Return m_val

                End Get

            End Property

 

            Public Sub New(ByVal val As String)

                MyBase.New("The string value cannot be converted to decimal.")

                m_val = val

            End Sub

 

        End Class

 

    End Class

posted @ 2005-03-30 14:33  妖居  阅读(4256)  评论(4编辑  收藏  举报