Qualcuno sa di un metodo per convertire parole come "prima", "decimo" e "un centesimo" in equivalente numerico?Esiste un metodo semplice per convertire una stringa numerica ordinale in un valore numerico corrispondente?
Campioni: "prima" -> 1, "secondo" -> 2, "decimo" -> 10, "centesimo" -> 100
qualsiasi algoritmo sarà sufficiente, ma sto scrivendo questo in C#.
EDIT
Non è bello e funziona solo con una parola alla volta, ma si adatta al mio scopo. Forse qualcuno può migliorarlo ma sono fuori tempo.
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 < 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 < 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;
}
Vuoi dire, convertire qualcosa come : '231' a' 1 + 30 + 200'? – Ruel
@Ruel, no, penso che l'OP indichi 1 -> "primo", 10 -> "decimo", 100 -> "un centesimo" –
quanto in alto vuoi andare con questo? – BlackICE