-1

hi guys i'm just wondering if you can help me with my program It is about "number to word conversion", i found a program for it but the problem is i need to display the CENTS which is a decimal value in numbers so how can i convert that decimal value into word?

EX. 150.25 = one hundred fifty and twenty five cents

I am using Visual Studio 2010

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
user3398069
  • 27
  • 1
  • 1
  • You found a program that only translates integers? – Magnus Mar 16 '14 at 09:49
  • Can you take a look at [convert-integers-to-written-numbers](http://stackoverflow.com/questions/3213/convert-integers-to-written-numbers) – Mohamed Abbas Mar 16 '14 at 09:49
  • well i'm still new to vb and our teacher just asked us to do this conversion without actually explaining anything about the program so it's quite hard to study this thing tnx for the suggestions anyway! ^^ – user3398069 Mar 17 '14 at 14:04

3 Answers3

1

Here is what i came up with quick google search : http://begeeben.wordpress.com/2012/03/20/convert-number-to-words-in-english/

Seems to do the job !

Private Function ConvertNumberToENG(ByVal amount As String) As String

    Dim dollars, cents, temp
    Dim decimalPlace, count
    Dim place(9) As String
    place(2) = " Thousand "
    place(3) = " Million "
    place(4) = " Billion "
    place(5) = " Trillion "

    ' String representation of amount.
    amount = amount.Trim()
    amount = amount.Replace(",", "")
    ' Position of decimal place 0 if none.
    decimalPlace = amount.IndexOf(".")
    ' Convert cents and set string amount to dollar amount.
    If decimalPlace > 0 Then
        cents = GetTens(amount.Substring(decimalPlace + 1).PadRight(2, "0").Substring(0, 2))
        amount = amount.Substring(0, decimalPlace).Trim()
    End If

    count = 1
    Do While amount <> ""
        temp = GetHundreds(amount.Substring(Math.Max(amount.Length, 3) - 3))
        If temp <> "" Then dollars = temp & place(count) & dollars
        If amount.Length > 3 Then
            amount = amount.Substring(0, amount.Length - 3)
        Else
            amount = ""
        End If
        count = count + 1
    Loop

    Select Case dollars
        Case ""
            dollars = "No Dollars"
        Case "One"
            dollars = "One Dollar"
        Case Else
            dollars = dollars & " Dollars"
    End Select

    Select Case cents
        Case ""
            cents = " and No Cents"
        Case "One"
            cents = " and One Cent"
        Case Else
            cents = " and " & cents & " Cents"
    End Select

    ConvertNumberToENG = dollars & cents
End Function

' Converts a number from 100-999 into text
Function GetHundreds(ByVal amount As String) As String
    Dim Result As String
    If Not Integer.Parse(amount) = 0 Then
        amount = amount.PadLeft(3, "0")
        ' Convert the hundreds place.
        If amount.Substring(0, 1) <> "0" Then
            Result = GetDigit(amount.Substring(0, 1)) & " Hundred "
        End If
        ' Convert the tens and ones place.
        If amount.Substring(1, 1) <> "0" Then
            Result = Result & GetTens(amount.Substring(1))
        Else
            Result = Result & GetDigit(amount.Substring(2))
        End If
        GetHundreds = Result
    End If
End Function

' Converts a number from 10 to 99 into text.
Private Function GetTens(ByRef TensText As String) As String
    Dim Result As String
    Result = ""           ' Null out the temporary function value.
    If TensText.StartsWith("1") Then   ' If value between 10-19...
        Select Case Integer.Parse(TensText)
            Case 10 : Result = "Ten"
            Case 11 : Result = "Eleven"
            Case 12 : Result = "Twelve"
            Case 13 : Result = "Thirteen"
            Case 14 : Result = "Fourteen"
            Case 15 : Result = "Fifteen"
            Case 16 : Result = "Sixteen"
            Case 17 : Result = "Seventeen"
            Case 18 : Result = "Eighteen"
            Case 19 : Result = "Nineteen"
            Case Else
        End Select
    Else                                 ' If value between 20-99...
        Select Case Integer.Parse(TensText.Substring(0, 1))
            Case 2 : Result = "Twenty "
            Case 3 : Result = "Thirty "
            Case 4 : Result = "Forty "
            Case 5 : Result = "Fifty "
            Case 6 : Result = "Sixty "
            Case 7 : Result = "Seventy "
            Case 8 : Result = "Eighty "
            Case 9 : Result = "Ninety "
            Case Else
        End Select
        Result = Result & GetDigit(TensText.Substring(1, 1))  ' Retrieve ones place.
    End If
    GetTens = Result
End Function

' Converts a number from 1 to 9 into text.
Private Function GetDigit(ByRef Digit As String) As String
    Select Case Integer.Parse(Digit)
        Case 1 : GetDigit = "One"
        Case 2 : GetDigit = "Two"
        Case 3 : GetDigit = "Three"
        Case 4 : GetDigit = "Four"
        Case 5 : GetDigit = "Five"
        Case 6 : GetDigit = "Six"
        Case 7 : GetDigit = "Seven"
        Case 8 : GetDigit = "Eight"
        Case 9 : GetDigit = "Nine"
        Case Else : GetDigit = ""
    End Select
End Function
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
1

So if you already have a program that can translate integers you can easily reuse that for decimals.

Dim d As Decimal = 150.25D
Dim arr = d.ToString(CultureInfo.InvariantCulture).Split("."C)
Dim phrase = runExistingProgram(Integer.Parse(arr(0))) + " dollars"
If arr.Length > 1 AndAlso arr(1) <> "0" Then
    phrase += " and " + runExistingProgram(Integer.Parse(arr(1))) + " cents"
End If
Magnus
  • 45,362
  • 8
  • 80
  • 118
0

Public Class ConverttoWord

Public Shared Function ConvertNum(ByVal Input As Decimal) As String


    Dim formatnumber As String
    Dim numparts(10) As String ' break the number into parts
    Dim suffix(10) As String 'trillion, billion .million etc
    Dim Wordparts(10) As String  'add the number parts and suffix

    Dim output As String = Nothing

    Dim T, B, M, TH, H, C As String

    formatnumber = Format(Input, "0000000000000.00") 'format the input number to a 16 characters string by suffixing and prefixing 0s
    '
    numparts(0) = primWord(Mid(formatnumber, 1, 1)) 'Trillion

    numparts(1) = primWord(Mid(formatnumber, 2, 1)) 'hundred billion..x
    numparts(2) = primWord(Mid(formatnumber, 3, 2)) 'billion

    numparts(3) = primWord(Mid(formatnumber, 5, 1)) 'hundred million...x
    numparts(4) = primWord(Mid(formatnumber, 6, 2)) 'million

    numparts(5) = primWord(Mid(formatnumber, 8, 1)) 'hundred thousand....x
    numparts(6) = primWord(Mid(formatnumber, 9, 2)) 'thousand


    numparts(7) = primWord(Mid(formatnumber, 11, 1)) 'hundred
    numparts(8) = primWord(Mid(formatnumber, 12, 2)) 'Tens

    numparts(9) = primWord(Mid(formatnumber, 15, 2)) 'cents



    suffix(0) = " Trillion "
        suffix(1) = " Hundred "  '....x
        suffix(2) = " Billion "
        suffix(3) = " Hundred " '  ....x
        suffix(4) = " Million "
        suffix(5) = " Hundred " ' .....x
        suffix(6) = " Thousand "
        suffix(7) = " Hundred "
    suffix(8) = " "
    suffix(9) = ""

    For i = 0 To 9
        If numparts(i) <> "" Then
            Wordparts(i) = numparts(i) & suffix(i)
        End If

        T = Wordparts(0)

        If Wordparts(1) <> "" And Wordparts(2) = "" Then
            B = Wordparts(1) & " Billion "
        Else
            B = Wordparts(1) & Wordparts(2)
        End If

        If Wordparts(3) <> "" And Wordparts(4) = "" Then
            M = Wordparts(3) & " Million "
        Else
            M = Wordparts(3) & Wordparts(4)
        End If

        If Wordparts(5) <> "" And Wordparts(6) = "" Then

            TH = Wordparts(5) & " Thousand "
        Else
            TH = Wordparts(5) & Wordparts(6)
        End If

        H = Wordparts(7) & Wordparts(8)
        If Wordparts(9) = "" Then
            C = " and  Zero Cents "
        Else
            C = " and " & Wordparts(9) & " Cents "
        End If
    Next
    output = T & B & M & TH & H & C
    Return output


End Function


Public Shared Function primWord(ByVal Num As Integer) As String

    'This two dimensional array store the primary word convertion of numbers 0 to 99
    primWord = ""
    Dim wordList(,) As Object = {{1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"}, {5, "Five"},
{6, "Six "}, {7, "Seven "}, {8, "Eight "}, {9, "Nine "}, {10, "Ten "}, {11, "Eleven "}, {12, "Twelve "}, {13, "Thirteen "},
{14, "Fourteen "}, {15, "Fifteen "}, {16, "Sixteen "}, {17, "Seventeen "}, {18, "Eighteen "}, {19, "Nineteen "},
{20, "Twenty "}, {21, "Twenty One "}, {22, "Twenty Two"}, {23, "Twenty Three"}, {24, "Twenty Four"}, {25, "Twenty Five"},
{26, "Twenty Six"}, {27, "Twenty Seven"}, {28, "Twenty Eight"}, {29, "Twenty Nine"}, {30, "Thirty "}, {31, "Thirty One "},
{32, "Thirty Two"}, {33, "Thirty Three"}, {34, "Thirty Four"}, {35, "Thirty Five"}, {36, "Thirty Six"}, {37, "Thirty Seven"},
{38, "Thirty Eight"}, {39, "Thirty Nine"}, {40, "Forty "}, {41, "Forty One "}, {42, "Forty Two"}, {43, "Forty Three"},
{44, "Forty Four"}, {45, "Forty Five"}, {46, "Forty Six"}, {47, "Forty Seven"}, {48, "Forty Eight"}, {49, "Forty Nine"},
{50, "Fifty "}, {51, "Fifty One "}, {52, "Fifty Two"}, {53, "Fifty Three"}, {54, "Fifty Four"}, {55, "Fifty Five"},
{56, "Fifty Six"}, {57, "Fifty Seven"}, {58, "Fifty Eight"}, {59, "Fifty Nine"}, {60, "Sixty "}, {61, "Sixty One "},
{62, "Sixty Two"}, {63, "Sixty Three"}, {64, "Sixty Four"}, {65, "Sixty Five"}, {66, "Sixty Six"}, {67, "Sixty Seven"}, {68, "Sixty Eight"},
{69, "Sixty Nine"}, {70, "Seventy "}, {71, "Seventy One "}, {72, "Seventy Two"}, {73, "Seventy Three"}, {74, "Seventy Four"},
{75, "Seventy Five"}, {76, "Seventy Six"}, {77, "Seventy Seven"}, {78, "Seventy Eight"}, {79, "Seventy Nine"},
{80, "Eighty "}, {81, "Eighty One "}, {82, "Eighty Two"}, {83, "Eighty Three"}, {84, "Eighty Four"}, {85, "Eighty Five"},
{86, "Eighty Six"}, {87, "Eighty Seven"}, {88, "Eighty Eight"}, {89, "Eighty Nine"}, {90, "Ninety "}, {91, "Ninety One "},
{92, "Ninety Two"}, {93, "Ninety Three"}, {94, "Ninety Four"}, {95, "Ninety Five"}, {96, "Ninety Six"}, {97, "Ninety Seven"},
{98, "Ninety Eight"}, {99, "Ninety Nine"}}

    Dim i As Integer
    For i = 0 To UBound(wordList)
        If Num = wordList(i, 0) Then
            primWord = wordList(i, 1)
            Exit For
        End If
    Next
    Return primWord
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    TextBoxWord.Text = ConverttoWord.ConvertNum(TextBoxfigure.Text)
End Sub

End Class

enter code here