在vb.net中以单词显示金额

时间:2014-02-27 06:18:14

标签: vb.net numbers

我的表格中有一个名为FeeAmount的列。

在我的网页中,我需要以单词显示金额,例如

Value in Column = 20000 
In Web Page - Twenty Thousand Only

有人可以帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:4)

试试这个

Function NumberToText(ByVal n As Integer) As String

   Select Case n
Case 0
  Return ""

Case 1 To 19
  Dim arr() As String = {"One","Two","Three","Four","Five","Six","Seven", _
    "Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen", _
      "Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}
  Return arr(n-1) & " "

Case 20 to 99
  Dim arr() as String = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}
  Return arr(n\10 -2) & " " & NumberToText(n Mod 10)

Case 100 to 199
  Return "One Hundred " & NumberToText(n Mod 100)

Case 200 to 999
  Return NumberToText(n\100) & "Hundreds " & NumberToText(n mod 100)

Case 1000 to 1999
  Return "One Thousand " & NumberToText(n Mod 1000)

Case 2000 to 999999
  Return NumberToText(n\1000) & "Thousands " & NumberToText(n Mod 1000)

Case 1000000 to 1999999
  Return "One Million " & NumberToText(n Mod 1000000)

Case 1000000 to 999999999
  Return NumberToText(n\1000000) & "Millions " & NumberToText(n Mod 1000000)

Case 1000000000 to 1999999999
  Return "One Billion " & NumberTotext(n Mod 1000000000)

Case Else
  Return NumberToText(n\1000000000) & "Billion " _
    & NumberToText(n mod 1000000000)
End Select
End Function

答案 1 :(得分:2)

看到此链接我在我的vb.net应用程序中使用此功能 http://pastebin.com/BjxDv7tR

答案 2 :(得分:1)

Public Function test(ByVal Amount As Double) As String
    mOnesArray(0) = "ONE"
    mOnesArray(1) = "TWO"
    mOnesArray(2) = "THREE"
    mOnesArray(3) = "FOUR"
    mOnesArray(4) = "FIVE"
    mOnesArray(5) = "SIX"
    mOnesArray(6) = "SEVEN"
    mOnesArray(7) = "EIGHT"
    mOnesArray(8) = "NINE"

    mOneTensArray(0) = "TEN"
    mOneTensArray(1) = "ELEVEN"
    mOneTensArray(2) = "TWELVE"
    mOneTensArray(3) = "THIRTEEN"
    mOneTensArray(4) = "FOURTEEN"
    mOneTensArray(5) = "FIFTEEN"
    mOneTensArray(6) = "SIXTEEN"
    mOneTensArray(7) = "SEVENTEEN"
    mOneTensArray(8) = "EIGHTEEN"
    mOneTensArray(9) = "NINETEEN"

    mTensArray(0) = "TWENTY"
    mTensArray(1) = "THIRTY"
    mTensArray(2) = "FORTY"
    mTensArray(3) = "FIFTY"
    mTensArray(4) = "SIXTY"
    mTensArray(5) = "SEVENTY"
    mTensArray(6) = "EIGHTY"
    mTensArray(7) = "NINETY"

    mPlaceValues(0) = "HUNDRED"
    mPlaceValues(1) = "THOUSAND"
    mPlaceValues(2) = "MILLION"
    mPlaceValues(3) = "BILLION"
    mPlaceValues(4) = "TRILLION"

    test = ConvertNumberToWords(Amount)
End Function

将此功能设为上述,然后调用它:

lbl_amtword.Text = func.test(final_salary) & "Only"

答案 3 :(得分:1)

Function readsingle(ByVal m As Byte) As String

    Dim st As String = ""
    Select Case m
        Case 1 : st = "One" : Case 2 : st = "Two"
        Case 3 : st = "Three" : Case 4 : st = "Four"
        Case 5 : st = "Five" : Case 6 : st = "Six"
        Case 7 : st = "Seven" : Case 8 : st = "Eight"
        Case 9 : st = "Nine" : Case 10 : st = "Ten"
        Case 11 : st = "Eleven" : Case 12 : st = "Twelve"
        Case 13 : st = "Thirteen" : Case 14 : st = "Fourteen"
        Case 15 : st = "Fifteen" : Case 16 : st = "Sixteen"
        Case 17 : st = "Seventeen" : Case 18 : st = "Eighteen"
        Case 19 : st = "Nineteen" : Case 20 : st = "Twenty"
    End Select
    readsingle = st
End Function

Function readtenths(ByVal m As Byte) As String
    Dim n As Double
    n = m \ 10
    Dim st As String = ""
    Select Case n
        Case 2 : st = "Twenty " : Case 3 : st = "Thirty "
        Case 4 : st = "Forty " : Case 5 : st = "Fifty "
        Case 6 : st = "Sixty " : Case 7 : st = "Seventy "
        Case 8 : st = "Eighty " : Case 9 : st = "Ninety "
    End Select
    readtenths = st
End Function
Function readcombined(ByVal m As Byte) As String
    Dim st As String = ""
    Dim n As Double
    If m < 21 Then
        st = readsingle(m)
    ElseIf m < 100 Then
        st = readtenths(m)
        n = m Mod (10)
        st = st & readsingle((n))
    End If
    readcombined = st
End Function
Function num2str(ByVal m As Double) As String
    'If m > 99999999999.0# Then Exit Function
    Dim n As Double
    n = Fix(m / 1000000000)
    Dim st As String = ""
    If m = 0 Or m < 0 Then
        st = " Z E R O"
        num2str = st
        Exit Function
    End If
    If n > 0 Then
        st = st & readcombined((n)) & " Arab "
    End If
    m = m - n * 1000000000
    n = m \ 10000000
    If n > 0 Then
        st = st & readcombined((n)) & " Crore "
    End If
    n = m Mod (10000000)
    n = n \ 100000
    If n > 0 Then
        st = st & readcombined((n)) & " lac "
    End If
    n = m Mod (100000)
    n = n \ 1000
    If n > 0 Then
        st = st & readcombined((n)) & " Thousand "
    End If
    n = m Mod (1000)
    n = n \ 100
    If n > 0 Then
        st = st & readsingle((n)) & " Hundred "
    End If
    n = m Mod (100)
    st = st & " " & readcombined((n))
    num2str = st
End Function

答案 4 :(得分:-1)

Public Class Form1
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    Dim y
End Sub
Function no(x As String) As String

    If (x = 0) Then

        Return ""

    ElseIf (x = 1) Then
        Return " One "

    ElseIf (x = 2) Then
        Return " Two "

    ElseIf (x = 3) Then
        Return " Three "

    ElseIf (x = 4) Then
        Return " Four "

    ElseIf (x = 5) Then
        Return " Five "

    ElseIf (x = 6) Then
        Return " Six "

    ElseIf (x = 7) Then
        Return " Seven "

    ElseIf (x = 8) Then
        Return " Eight "

    ElseIf (x = 9) Then
        Return " Nein "


    ElseIf (x = 10) Then
        Return " Ten "

    ElseIf (x = 11) Then
        Return " Eleven "

    ElseIf (x = 12) Then
        Return " Twelve "

    ElseIf (x = 13) Then
        Return " Thirteen "

    ElseIf (x = 14) Then
        Return " Forteen "

    ElseIf (x = 15) Then
        Return " Fiveteen "

    ElseIf (x = 16) Then
        Return " Sixteen "

    ElseIf (x = 17) Then
        Return " Seventeen "

    ElseIf (x = 18) Then
        Return " Eightteen "

    ElseIf (x = 19) Then
        Return " Ninteen "


    End If
End Function
Function ten(y As Integer) As String

    If (y <= 19) Then

        Dim x = y
        Dim s = no(x.ToString)
        Return (s)

    ElseIf (y <= 29) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
       Return (" Twenty" + s)
    ElseIf (y <= 39) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)

        Return (" Thirty" + s)

   ElseIf (y <= 49) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
        Return (" Fourty" + s)



    ElseIf (y <= 59) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
        Return (" Fivfty" + s)
    ElseIf (y <= 69) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
        Return (" Sixty" + s)



    ElseIf (y <= 79) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
        Return (" Seventy" + s)

    ElseIf (y <= 89) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
        Return (" Eighty" + s)

    ElseIf (y <= 99) Then
        Dim arr As Array = y.ToString.ToArray
        Dim z = arr(0)
        Dim x = arr(1)
        Dim s = no(x.ToString)
        Return (" Ninty" + s)
       End If
    End Function

Function zero(z As Integer) As String
    If (z > 0) Then
        Dim arr As Array = TextBox1.Text.ToArray
        Dim a = arr(0)
        If (a = 0) Then
            TextBox2.Text = "invalid no."
        End If
    End If
End Function
Function fir(z As Integer) As String


    If (z >= 1 & z <= 19) Then

        Dim x = z
        Dim s = ten(x.ToString)
        TextBox2.Text = (s)
    End If

End Function
Function two(z As Integer) As String
    If (z >= 1 & z <= 99) Then

        Dim y = z

        Dim t = ten(y.ToString)

        TextBox2.Text = (t)
    End If

End Function
Function hun(z As Integer) As String
    Dim arr As Array = TextBox1.Text.ToArray
    Dim w = arr(0)
    If (z >= 100 & z <= 999) Then
        Dim l = arr(1)
        Dim o = arr(2)
        Dim y = (l + o)

        Dim x = arr(0)
        Dim s = no(x.ToString)

        If (y <= 99) Then

            Dim i = ten(y.ToString)
            TextBox2.Text = (s + " Hundred " + i)
        End If

    End If

End Function
Function thousen(z As Integer) As String
    If (z >= 1000 & z <= 9999) Then
        Dim arr As Array = TextBox1.Text.ToArray
        Dim x = arr(0)
        Dim k = arr(1)
        Dim l = arr(2)
        Dim o = arr(3)
        Dim y = (l + o)
        Dim s = no(x.ToString)
        Dim u = no(k.ToString)
        If (y <= 99) Then

            Dim i = ten(y.ToString)
            TextBox2.Text = (s + " Thousen " + u + " Hundred " + i)
        End If

    End If

End Function
Function tenthou(z As Integer) As String
    Dim arr As Array = TextBox1.Text.ToArray

    If (z >= 10000 & z <= 99999) Then
        Dim w = arr(0)
        Dim v = arr(1)
        Dim e = (w + v)
        Dim x = arr(2)
        Dim q = arr(3)
        Dim k = arr(4)
        Dim f = (q + k)
        Dim y = (e) & (f)
        Dim t = ten(e)
        Dim t2 = ten(f)

        Dim s = no(x.ToString)
        TextBox2.Text = (t + "thousen" + s + "hundred" + t2)
    End If

End Function
Function onelaks(z As Integer) As String
    Dim arr As Array = TextBox1.Text.ToArray
    If (z >= 100000 & z <= 999999) Then
        Dim a = arr(0)
        Dim b = arr(1)
        Dim c = arr(2)
        Dim d = arr(3)
        Dim e = arr(4)
        Dim f = arr(5)
        Dim s = no(a.ToString)
        Dim t = (b) & (c)
        Dim u = no(d.ToString)
        Dim v = (e) & (f)
        Dim t1 = ten(t)
        Dim v1 = ten(v)
        TextBox2.Text = (s + "laks" + t1 + "thousend" + u + "hundread" + v1)
    End If
End Function
Function tenlaks(z As Integer) As String
    Dim arr As Array = TextBox1.Text.ToArray
    If (z >= 1000000 & z <= 9999999) Then
        Dim a = arr(0)
        Dim b = arr(1)
        Dim c = arr(2)
        Dim d = arr(3)
        Dim e = arr(4)
        Dim f = arr(5)
        Dim g = arr(6)
        Dim s = (a) & (b)
        Dim t = (c) & (d)
        Dim u = no(e.ToString)
        Dim v = (f) & (g)
        Dim s1 = ten(s)
        Dim t1 = ten(t)
        Dim v1 = ten(v)
        TextBox2.Text = (s1 + "laks" + t1 + "thousend" + u + "hundread" + v1)
    End If
End Function
Function onecrore(z As Integer) As String
    Dim arr As Array = TextBox1.Text.ToArray
    If (z >= 10000000 & z <= 99999999) Then
        Dim a = arr(0)
        Dim b = arr(1)
        Dim c = arr(2)
        Dim d = arr(3)
        Dim e = arr(4)
        Dim f = arr(5)
        Dim g = arr(6)
        Dim h = arr(7)
        Dim s = no(a.ToString)
        Dim t = (b) & (c)
        Dim u = (d) & (e)
        Dim v = no(f.ToString)
        Dim w = (g) & (h)
        Dim t1 = ten(t)
        Dim u1 = ten(u)
        Dim w1 = ten(w)
        TextBox2.Text = (s + "crore" + t1 + "laks" + u1 + "thousend" + v + "hundread" + w1)
    End If
End Function
Function tencrore(z As Integer) As String
    Dim arr As Array = TextBox1.Text.ToArray
    If (z >= 100000000 & z <= 999999999) Then
        Dim a = arr(0)
        Dim b = arr(1)
        Dim c = arr(2)
        Dim d = arr(3)
        Dim e = arr(4)
        Dim f = arr(5)
        Dim g = arr(6)
        Dim h = arr(7)
        Dim i = arr(8)
        Dim s = (a) & (b)
        Dim t = (c) & (d)
        Dim u = (e) & (f)
        Dim v = no(g.ToString)
        Dim w = (h) & (i)
        Dim s1 = ten(s)
        Dim t1 = ten(t)
        Dim u1 = ten(u)
        Dim w1 = ten(w)
        TextBox2.Text = (s1 + "crore" + t1 + "laks" + u1 + "thousend" + v + "hundread" + w1)
    End If
End Function
Function main(z As Integer) As String
    If (z >= 0) Then
        Dim e = zero(z)
    ElseIf (z >= 1 & z <= 19) Then
        Dim a = fir(z)
    ElseIf (z <= 99) Then
        Dim b = two(z)
    ElseIf (z <= 999) Then
        Dim c = hun(z)
    ElseIf (z <= 9999) Then
        Dim d = thousen(z)
    ElseIf (z <= 99999) Then
        Dim d = tenthou(z)
    ElseIf (z <= 999999) Then
        Dim d = onelaks(z)
    ElseIf (z <= 9999999) Then
        Dim d = tenlaks(z)
    ElseIf (z <= 99999999) Then
        Dim d = onecrore(z)
    ElseIf (z <= 999999999) Then
        Dim d = tencrore(z)
    End If

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim z = TextBox1.Text
    Dim g = main(z)
    End Sub
End Class