m1ks
m1ks

Reputation: 79

Convert Numbers to Words using only SELECT CASE in vb.net

I'm new in the environment of vb.net. So, our professor asks us to convert numbers to words using only Select Case. We are only allowed using SELECT CASE -- If, If else, arrays and such are NOT allowed. The problem is I do it in a "hard code" way and I want it to be more "clean" and efficient and maximum numbers are up to 5 digits. I hope someone can give me bright ideas on this.

    Dim one As String = "One"
    Dim two As String = "Two"
    Dim three As String = "Three"
    Dim four As String = "Four"
    Dim five As String = "Five"
    Dim six As String = "Six"
    Dim seven As String = "Seven"
    Dim eight As String = "Eight"
    Dim nine As String = "Nine"
    Dim ten As String = "Ten "
    Dim twenty As String = "Twenty "
    Select Case input >= 20 And input <= 99
        Case True
            Select Case input
                Case 20
                    lblOutput.Text = twenty
                Case 21
                    lblOutput.Text = twenty + one
                Case 22
                    lblOutput.Text = twenty + two
                Case 23
                    lblOutput.Text = twenty + three

Upvotes: 1

Views: 1644

Answers (1)

Chase Rocker
Chase Rocker

Reputation: 1908

It can be done with 30 Case Statements and 29 literal word strings. This is not the complete solution, you'll need to add more logic.

These are the distinct words needed to represent the numbers from 1 to 99,999:

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
thirty
fourty
fifty
sixty
seventy
eighty
ninety
hundred
thousand

one to nineteen is needed when input is 1 to 19

twenty to ninety is needed when input is 20 to 99

hundred is needed when input is 100 to 999

thousand is needed when input >= 1000

So for example, if the number is 1384, break it up into parts and recursively call the same function to return the words.

    Private Function convertToWords(input As Integer) As String
        Dim words As String = ""

        Select Case input
            Case 1
                words = "one"

            Case Is >= 1000
                Dim thousands As Integer = (input \ 1000) '<= how many 1000's are there?
                Select Case thousands
                    Case Is > 0
                        input = (input Mod 1000) '<= the remainder is the new value which will be used by calling the same function again
                        words &= convertToWords(thousands) & " thousand " & convertToWords(input)
                End Select    
        End Select

        Return words
    End Function

There's 1 thousand and the remainder is 384 which becomes the new input and it will be passed into the same function and it then gets split into hundreds the same way.

Once you add the Case Statements for 1 to 19, 20,30,40... and 100 to 999, it will change the 1 to one thousand and 3 to three hundred and 80 to eighty and 4 to four as it's recursively calling itself.

Also keep in mind, you don't need a case statement for 84, you just need one for 80 and one for 4 because you're splitting it up. So if it's 85, it will use the same 80 case and the 5 case when called recursively.

Upvotes: 1

Related Questions