RSS

將阿拉伯數字金額轉換成國字金額

08 Mar

處理銀行支票的時候,需要將阿拉伯數字轉換為國字,以下這個function可以將一般的金額轉換為國字金額,數值可以包含”,”和”.”,但小數點下必須是零:

''' <summary>
''' 將阿拉伯數字金額轉換成國字金額
''' </summary>
''' <param name="amount">阿拉伯數字金額</param>
''' <returns>國字金額</returns>
''' <remarks></remarks>
Private Function ConvertNumberToCHT(ByVal amount As String) As String

    Dim result As New System.Text.StringBuilder

    ' 判斷參數是不是數值
    Dim fraction As Double
    If Double.TryParse(amount, fraction) Then

        ' 判斷小數位以下是不是0
        If fraction - Math.Truncate(fraction) = 0 Then

            Dim NumPos() As Char = {"零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖"}
            Dim CenPos() As Char = {"仟", "", "拾", "佰"}
            Dim ComPos() As Char = {"", "萬", "億", "兆", "京"}

            Dim number As Long = Math.Truncate(fraction)
            Dim n As Long = number
            Dim numberLength As Integer = 1
            Do While n >= 10
                numberLength += 1
                n = n \ 10
            Loop

            Dim digits(numberLength - 1) As Integer
            For i As Integer = 1 To numberLength
                digits(i - 1) = number Mod 10
                number = number \ 10
            Next
            Dim remainder As Integer
            Dim previousDigit As Integer = digits(numberLength - 1)

            ' 轉換left most digit
            result.Append(NumPos(digits(numberLength - 1)))

            For i As Integer = numberLength - 1 To 1 Step -1

                remainder = digits(i - 1)

                If Not previousDigit = 0 Then
                    If Not (i + 1) Mod 4 = 1 Then
                        result.Append(CenPos((i + 1) Mod 4))
                    End If
                End If

                If (i + 1) Mod 4 = 1 Then
                    Dim testSum As Integer = 0
                    Dim index As Integer = 1
                    Do While (index < 5) And (i + index <= numberLength)
                        testSum += digits(i + index - 1)
                        index += 1
                    Loop

                    If Not testSum = 0 Then
                        result.Append(ComPos((i + 1) \ 4))
                    End If
                End If

                If Not remainder = 0 Then
                    If previousDigit = 0 And Not (i + 1) Mod 4 = 1 Then
                        result.Append(NumPos(0))
                    End If
                    result.Append(NumPos(remainder))
                End If

                previousDigit = remainder

            Next

            result.Append("元整")

        Else
            Throw New Exception("傳入的參數非整數")
        End If

    Else
        Throw New Exception("傳入的參數非數值")
    End If

    Return result.ToString()

End Function
Advertisements
 
Leave a comment

Posted by on March 8, 2012 in .NET, VB

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: