Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a simple method of converting an ordinal numeric string to its matching numeric value?
    primarykey
    data
    text
    <p>Does anyone know of a method to convert words like "first", "tenth" and "one hundredth" to their numeric equivalent?</p> <p>Samples: "first" -> 1, "second" -> 2, "tenth" -> 10, "hundredth" -> 100</p> <p>Any algorithm will suffice but I'm writing this in C#.</p> <p>EDIT</p> <p>It ain't pretty and only works with one word at a time but it suits my purposes. Maybe someone can improve it but I'm out of time.</p> <pre><code> public static int GetNumberFromOrdinalString(string inputString) { string[] ordinalNumberWords = { "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth" }; string[] ordinalNumberWordsTens = { "", "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" }; string[] ordinalNumberWordsExtended = {"hundredth", "thousandth", "millionth", "billionth" }; if (inputString.IsNullOrEmpty() || inputString.Length &lt; 5 || inputString.Contains(" ")) return 0; if (ordinalNumberWords.Contains(inputString) || ordinalNumberWordsTens.Contains(inputString)) { var outputMultiplier = ordinalNumberWords.Contains(inputString) ? 1 : 10; var arrayToCheck = ordinalNumberWords.Contains(inputString) ? ordinalNumberWords : ordinalNumberWordsTens; // Use the loop counter to get our output integer. for (int x = 0; x &lt; arrayToCheck.Count(); x++) { if (arrayToCheck[x] == inputString) { return x * outputMultiplier; } } } // Check if the number is one of our extended numbers and return the appropriate value. if (ordinalNumberWordsExtended.Contains(inputString)) { return inputString == ordinalNumberWordsExtended[0] ? 100 : inputString == ordinalNumberWordsExtended[1] ? 1000 : inputString == ordinalNumberWordsExtended[2] ? 1000000 : 1000000000; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload