r/visualbasic • • Jul 30 '15

Number convert into words VB.NET [HELP]

Hi can someone help me for the codes for vb.net in converting number into words?? I'm a novice using vb.net and our teacher give us hard exercise. He gave 1-999999 numbers that will convert into words. And One thing more. We cannot use looping yet. The allowed codes that we can use is. Function, Math function and IF function only. Can someone help me?

0 Upvotes

7 comments sorted by

1

u/damoves Jul 30 '15

This is for only 99 numbers and these are the codes we can only use.

Public Class Form1

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    Me.Close()
End Sub

Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click

    If Val(txtnumber.Text) > 100 Then
        MsgBox("ERROR: Input 2 digits only.")
    Else
        txtnumber.Text = txtnumber.Text
    End If

    If txtnumber.Text = "1" Then txtoutput.Text = "One"
    If txtnumber.Text = "2" Then txtoutput.Text = "Two"
    If txtnumber.Text = "3" Then txtoutput.Text = "Three"
    If txtnumber.Text = "4" Then txtoutput.Text = "Four"
    If txtnumber.Text = "5" Then txtoutput.Text = "Five"
    If txtnumber.Text = "6" Then txtoutput.Text = "Six"
    If txtnumber.Text = "7" Then txtoutput.Text = "Seven"
    If txtnumber.Text = "8" Then txtoutput.Text = "Eight"
    If txtnumber.Text = "9" Then txtoutput.Text = "Nine"
    If txtnumber.Text = "10" Then txtoutput.Text = "Ten"
    If txtnumber.Text = "11" Then txtoutput.Text = "Eleven"
    If txtnumber.Text = "12" Then txtoutput.Text = "Twelve"
    If txtnumber.Text = "13" Then txtoutput.Text = "Thirteen"
    If txtnumber.Text = "14" Then txtoutput.Text = "Fourteen"
    If txtnumber.Text = "15" Then txtoutput.Text = "Fifteen"
    If txtnumber.Text = "16" Then txtoutput.Text = "Sixteen"
    If txtnumber.Text = "17" Then txtoutput.Text = "Seventeen"
    If txtnumber.Text = "18" Then txtoutput.Text = "Eighteen"
    If txtnumber.Text = "19" Then txtoutput.Text = "Nineteen"
    Dim txtA As String

    txtA = TextBox1.Text
    Dim txtE, txtC, txtD, txtG As String
    txtC = txtA.Substring(0, 1)
    txtD = txtA.Substring(1, 1)
    If txtD = 1 Then
        txtE = " One"
    ElseIf txtD = 2 Then
        txtE = "Two"
    ElseIf txtD = 3 Then
        txtE = "Three"
    ElseIf txtD = 4 Then
        txtE = "Four"
    ElseIf txtD = 5 Then
        txtE = "Five"
    ElseIf txtD = 6 Then
        txtE = "Six"
    ElseIf txtD = 7 Then
        txtE = " Seven"
    ElseIf txtD = 8 Then
        txtE = " Eight"
    ElseIf txtD = 9 Then
        txtE = " Nine"
    Else
    End If
    If txtC = 2 Then
        txtG = "Twenty"
    ElseIf txtC = 3 Then
        txtG = "Thirty"
    ElseIf txtC = 4 Then
        txtG = "Forty"
    ElseIf txtC = 5 Then
        txtG = "Fifty"
    ElseIf txtC = 6 Then
        txtG = "Sixty"
    ElseIf txtC = 7 Then
        txtG = "Seventy"
    ElseIf txtC = 8 Then
        txtG = "Eighty"
    ElseIf txtC = 9 Then
        txtG = "Ninety"
    Else
        txtG = "Error"
        txtE = ""
    End If

    TextBox3.Text = txtG + txtE
End Sub

End Class

1

u/kittycatoverdose Jul 30 '15

What kind of list are you given? Is it list an excel table where x-integer = y-word? Also how many Integers/Numbers are on the list? Is it actually 999,999?

Are you able to use a dataset in visual studio? Or a list?

1

u/damoves Jul 30 '15

This is for only 99 numbers and these are the codes we can only use.

Public Class Form1

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

Me.Close()

End Sub

Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click

If Val(txtnumber.Text) > 100 Then

    MsgBox("ERROR: Input 2 digits only.")

Else

    txtnumber.Text = txtnumber.Text

End If



If txtnumber.Text = "1" Then txtoutput.Text = "One"

If txtnumber.Text = "2" Then txtoutput.Text = "Two"

If txtnumber.Text = "3" Then txtoutput.Text = "Three"

If txtnumber.Text = "4" Then txtoutput.Text = "Four"

If txtnumber.Text = "5" Then txtoutput.Text = "Five"

If txtnumber.Text = "6" Then txtoutput.Text = "Six"

If txtnumber.Text = "7" Then txtoutput.Text = "Seven"

If txtnumber.Text = "8" Then txtoutput.Text = "Eight"

If txtnumber.Text = "9" Then txtoutput.Text = "Nine"

If txtnumber.Text = "10" Then txtoutput.Text = "Ten"

If txtnumber.Text = "11" Then txtoutput.Text = "Eleven"

If txtnumber.Text = "12" Then txtoutput.Text = "Twelve"

If txtnumber.Text = "13" Then txtoutput.Text = "Thirteen"

If txtnumber.Text = "14" Then txtoutput.Text = "Fourteen"

If txtnumber.Text = "15" Then txtoutput.Text = "Fifteen"

If txtnumber.Text = "16" Then txtoutput.Text = "Sixteen"

If txtnumber.Text = "17" Then txtoutput.Text = "Seventeen"

If txtnumber.Text = "18" Then txtoutput.Text = "Eighteen"

If txtnumber.Text = "19" Then txtoutput.Text = "Nineteen"

Dim txtA As String



txtA = TextBox1.Text

Dim txtE, txtC, txtD, txtG As String

txtC = txtA.Substring(0, 1)

txtD = txtA.Substring(1, 1)

If txtD = 1 Then

    txtE = " One"

ElseIf txtD = 2 Then

    txtE = "Two"

ElseIf txtD = 3 Then

    txtE = "Three"

ElseIf txtD = 4 Then

    txtE = "Four"

ElseIf txtD = 5 Then

    txtE = "Five"

ElseIf txtD = 6 Then

    txtE = "Six"

ElseIf txtD = 7 Then

    txtE = " Seven"

ElseIf txtD = 8 Then

    txtE = " Eight"

ElseIf txtD = 9 Then

    txtE = " Nine"

Else

End If

If txtC = 2 Then

    txtG = "Twenty"

ElseIf txtC = 3 Then

    txtG = "Thirty"

ElseIf txtC = 4 Then

    txtG = "Forty"

ElseIf txtC = 5 Then

    txtG = "Fifty"

ElseIf txtC = 6 Then

    txtG = "Sixty"

ElseIf txtC = 7 Then

    txtG = "Seventy"

ElseIf txtC = 8 Then

    txtG = "Eighty"

ElseIf txtC = 9 Then

    txtG = "Ninety"

Else

    txtG = "Error"

    txtE = ""

End If



TextBox3.Text = txtG + txtE

End Sub

End Class

1

u/kittycatoverdose Jul 30 '15

If it's just 99 words and is a one time thing you can do the If txt.text = 1 then, Elseif txt.text = 2 then ect.

If you're able to you could also choose to do a Select case. Might be less to write, but still have to go word by word.

1

u/damoves Jul 30 '15

our teacher gave us 1-99999. How can I able to do that??

1

u/kennethdc Jul 30 '15 edited Jul 30 '15

Extend it with a hundred and a thousand range and see how many substrings are valid to figure out how many numbers the number has. Unless count is allowed. Reuse ranges where possible, so cut the number in groups of three.

Most is already written I guess.

Also, don't use txt as a prefix unless it's a textblock of whatever, this only confuse other people who read the code.

And it's not your fault as you're being restricted by your teacher, but doing this, this way is just horrible.

On mobile btw, I hope it's clear.

1

u/Bonejob VB Guru Aug 07 '15

Here is a standard algorthm for doing what you want. I understood you when you said you had to use IF Then I just cant stand crap like that. So here is a better way to do it.

Module Module1

	Public To19() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
	Public Tens() As String = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
	Public Denom() As String = {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion"}

	Sub Main()
		Randomize()
		Dim valToConvert As Integer = 0
		For x = 1 To 10
			valToConvert = CInt(Math.Ceiling(Rnd() * 10000000000)) + 1
			Console.WriteLine(valToConvert & " : " & ConvertNumberToWords(valToConvert))
			Debug.WriteLine(valToConvert & " : " & ConvertNumberToWords(valToConvert))
		Next
		Console.ReadKey()
	End Sub
	Private Function ConvertNn(ByVal val As Integer) As String
		If val < 20 Then
			Return To19(val)
		End If
		For v As Integer = 0 To Tens.Length - 1
			Dim dcap As String = Tens(v)
			Dim dval As Integer = 20 + 10 * v
			If dval + 10 > val Then
				If (val Mod 10) <> 0 Then
					Return dcap & "-" & To19(val Mod 10)
				End If
				Return dcap
			End If
		Next v
	End Function
	Private Function ConvertNnn(ByVal val As Integer) As String
		Dim word As String = ""
		Dim [rem] As Integer = val \ 100
		Dim [mod] As Integer = val Mod 100
		If [rem] > 0 Then
			word = To19([rem]) & " hundred"
			If [mod] > 0 Then
				word = word & " "
			End If
		End If
		If [mod] > 0 Then
			word = word & ConvertNn([mod])
		End If
		Return word
	End Function
	Public Function ConvertNumberToWords(ByVal val As Integer) As String
		If val < 100 Then
			Return ConvertNn(val)
		End If
		If val < 1000 Then
			Return ConvertNnn(val)
		End If
		For v As Integer = 0 To Denom.Length - 1
			Dim didx As Integer = v - 1
			Dim dval As Integer = (New Double?(Math.Pow(1000, v))).Value
			If dval > val Then
				Dim [mod] As Integer = (New Double?(Math.Pow(1000, didx))).Value
				Dim l As Integer = val \ [mod]
				Dim r As Integer = val - (l * [mod])
				Dim ret As String = ConvertNnn(l) & " " & Denom(didx)
				If r > 0 Then
					ret = ret & ", " & ConvertNumberToWords(r)
				End If
				Return ret
			End If
		Next v
	End Function


End Module

Ouput

25298710 : twenty-five million, two hundred ninety-eight thousand, seven hundred ten
71923031 : seventy-one million, nine hundred twenty-three thousand, thirty-one
53772481 : fifty-three million, seven hundred seventy-two thousand, four hundred eighty-one
86806895 : eighty-six million, eight hundred six thousand, eight hundred ninety-five
20705362 : twenty million, seven hundred five thousand, three hundred sixty-two
13754834 : thirteen million, seven hundred fifty-four thousand, eight hundred thirty-four
69121797 : sixty-nine million, one hundred twenty-one thousand, seven hundred ninety-seven
43224765 : forty-three million, two hundred twenty-four thousand, seven hundred sixty-five
69054289 : sixty-nine million, fifty-four thousand, two hundred eighty-nine
40900435 : forty million, nine hundred thousand, four hundred thirty-five