-1

I think my question is fairly simple but i just cant seem to get it to work. I have to change my variable from the string "One" to the integer 1 looks like this

BeginningValue = Int32.Parse(numbers(index))

I have also tried

BeginningValue = Convert.ToInt32(numbers(index))

but that doesnt seem to work either. Both of these lines just give me the error "System.FormatException: Input string was not in a correct format."

  • 1
    There's not a built-in way to do this. You have to write your own code to parse the text. – Joel Coehoorn Sep 13 '21 at 20:50
  • Check if any answers help you handle this issue and if it helps, please [consider accepting it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). If not, please feel free to let us know. – Xingyu Zhao Sep 23 '21 at 08:34

3 Answers3

4

There is no built-in library to convert words to numbers.

If you have a finite list of words that you want to convert, then you can store them in a dictionary and get the value by its key. E.g.:

Dim wordsToNumber = New Dictionary(Of String, Integer)() From {
    { "One", 1 },
    { "Two", 2 },
    { "Three", 3 }
}

Dim input = "One"
If (wordsToNumber.ContainsKey(input)) Then
    Dim beginningValue = wordsToNumber(input)
    Console.WriteLine(beginningValue)
End If

However, if you want to allow for any combination (e.g. One Thousand Twenty Five) then it is best to go with an existing algorithm. Here is a C# example that can be put through a converter: https://www.c-sharpcorner.com/Blogs/convert-words-to-numbers-in-c-sharp

David
  • 5,877
  • 3
  • 23
  • 40
1

Another option is to use Enum.

Enum Numbers
    One = 1
    Two = 2
    Three = 3
End Enum

Sub foo()
    Dim input = "one"
    Dim number = [Enum].Parse(GetType(Numbers), input, True)
    Console.WriteLine($"input: {input}, number: {number}, value: {CInt(number)}")
End Sub

Console:

input: one, number: One, value: 1

This will not be scalable at runtime such as David's Dictionary is i.e. the Dictionary could be fed its values from a file, whereas the Enum is compiled with a limited number of values. But you do get intellisense if other use cases are relevant.

enter image description here

djv
  • 15,168
  • 7
  • 48
  • 72
0

You can create an extension method for String(Code from here).

Module MyExtensions
<Extension()>
Function ParseWordsToNumber(ByVal number As String) As Integer
    Dim words As String() = number.ToLower().Split(New Char() {" "c, "-"c, ","c}, StringSplitOptions.RemoveEmptyEntries)
    Dim ones As String() = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
    Dim teens As String() = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
    Dim tens As String() = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
    Dim modifiers As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)() From {
        {"billion", 1000000000},
        {"million", 1000000},
        {"thousand", 1000},
        {"hundred", 100}
    }
    If number = "eleventy billion" Then Return Integer.MaxValue
    Dim result As Integer = 0
    Dim currentResult As Integer = 0
    Dim lastModifier As Integer = 1

    For Each word As String In words

        If modifiers.ContainsKey(word) Then
            lastModifier = lastModifier * modifiers(word)
        Else
            Dim n As Integer

            If lastModifier > 1 Then
                result += currentResult * lastModifier
                lastModifier = 1
                currentResult = 0
            End If

            If Array.IndexOf(ones, word) + 1 > 0 Then
                n = Array.IndexOf(ones, word) + 1
                currentResult += n
            ElseIf Array.IndexOf(teens, word) + 1 > 0 Then
                n = Array.IndexOf(teens, word) + 1
                currentResult += n + 10
            ElseIf Array.IndexOf(tens, word) + 1 > 0 Then
                n = Array.IndexOf(tens, word) + 1
                currentResult += n * 10
            ElseIf word <> "and" Then
                Throw New ApplicationException("Unrecognized word: " & word)
            End If
        End If
    Next

    Return result + currentResult * lastModifier
End Function
End Module

Then you can use it like:

BeginningValue = numbers(Index).ParseWordsToNumber
Xingyu Zhao
  • 625
  • 7
  • 27