Taffs
Taffs

Reputation: 153

Converting numbers to text (2 existent words, same number)

I'm trying to convert the numerical result into words.

The sample I found works well for English words, but as I change them to my language, bunch of superficial problems came out.

What I need help in particular with is building the same code for 100 as its for 10. The reason I am doing this is because in my language we don't have One standing in front of every Hundred/Thousand

For example: Let's say the first number in Ones is called Taff

As it is, the program will make Taff Hundred where it should make make something like Taffss Hundred

So I created the Hundreds() which includes the correct callings.

Here is my code :

intAmount = eAmount

Dim nTousands As Integer = intAmount \ 1000 : intAmount = intAmount Mod 1000
Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount \ 1

     If nTen > 0 Then
          If nTen = 1 And nOne > 0 Then
               wAmount = wAmount & Teens(nOne) & " "
          Else
               wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, " и ", " ")
               If nOne > 0 Then wAmount = wAmount & Ones(nOne) & "  "
               End If
          End If

         ElseIf nOne > 0 Then
              wAmount = wAmount & Ones(nOne) & " "
              wAmount = wAmount & HMBT(nSet) & " "
              wAmount = AmountInWords(CStr(CLng(nAmount) - _
              (eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)

Being the noob I am, I figured that by copy pasting the code for 10s, and changing the values to 100s will make things work, but it only works for 100 200 300 etc, not for the numbers in-between.

              ElseIf nHundred > 0 Then
                  If nHundred = 1 And nOne > 0 Then
                  'Don't know how to properly add things here.
                  Else
                       wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
                  If nOne > 0 Then wAmount = wAmount & Ones(nOne) & "  "
                  End If

Update I made some changes, this time it partially works... It shows 100s, 200s, 300s etc etc. But it only shows from 100 to 109 when it "increments" If it goes above 109, the 100s aren't show, and it goes back to showing 10s, 11s, 12s etc.

                ElseIf nHundred > 0 Then

                If nHundred = 1 And nTen > 0 And nOne > 0 Then
                    wAmount = wAmount & Teens(nTen) & " "

                Else
                    wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
                    If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "

                End If

Upvotes: 1

Views: 101

Answers (1)

Scott S
Scott S

Reputation: 90

Made some changes... Is this closer to what you're after?

    Private list_ones() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
    Private list_teens() As String = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}

    Private list_tens() As String = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}
    Private list_hundreds() As String = {"ones", "two", "three", "four", "five", "six", "seven", "eight", "nine"}

    Private Function get_string_from_numeric(ByVal curr_number As Integer) As String

        Dim to_return As String = ""

        Try
            Select Case curr_number
                Case Is < 10
                    to_return = list_ones(curr_number)
                Case 10 To 19
                    to_return = list_teens(curr_number - 10)
                Case 20 To 99
                    to_return = get_return_value(curr_number, 10, list_tens)
                Case 100 To 999
                    to_return = get_return_value(curr_number, 100, list_hundreds, "hundred")
                Case 1000 To 9999
                    to_return = get_return_value(curr_number, 1000, list_hundreds, "thousand")
                Case Is > 9999
                    to_return = "out of range"
            End Select
        Catch
        Finally
        End Try

        Return to_return

    End Function


    Private Function get_return_value(ByVal curr_number As Integer, ByVal curr_pace_holder As Integer, ByVal curr_array_list As String(), Optional ByVal str_term As String = "") As String
        Dim to_return As String = ""

        Dim curr_iter As Integer = Math.Floor(curr_number / curr_pace_holder)
        Dim curr_remainder As Integer = curr_number - curr_iter * curr_pace_holder
        If (str_term <> "") Then str_term = " " & str_term

        If (curr_remainder > 0) Then
            to_return = curr_array_list(curr_iter - 1) & str_term & " " & get_string_from_numeric(curr_remainder)
        Else
            to_return = curr_array_list(curr_iter - 1) & str_term
        End If

        Return to_return
    End Function


    Private Sub cmd_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_submit.Click

        Try
            If (IsNumeric(txt_input.Text)) Then
                txt_output.Text = get_string_from_numeric(CInt(txt_input.Text))
            Else
                MsgBox("Invalid input.")
            End If
        Catch
        Finally
        End Try

    End Sub

Upvotes: 1

Related Questions