I'm trying to write a practical function to convert an input with Roman Numerals into Decimal. I did it before already in 2 directions via Javascript, but in the C# version I have an issue with the while iteration in my loop, and I don´t know yet how to get this working.
class ToRoman
{
public static int RomanToDecimal(string romanNums)
{
int result = 0;
int[] deci = new int[] {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string[] roman = new string[] {"M", "CM", "D", "CD", "C", "XD", "L", "XL", "X", "IX", "V", "IV", "I"};
for (int i = 0; i < deci.Length; i++)
{
while (romanNums.IndexOf(roman[i] == romanNums[i])
{
result += deci[i];
romanNums = romanNums.Replace(roman[i]," ");
}
}
return result;
}
static void Main()
{
Console.WriteLine(RomanToDecimal("V"));
Console.WriteLine(RomanToDecimal("XIX"));
Console.WriteLine(RomanToDecimal("MDXXVI"));
Console.WriteLine(RomanToDecimal("MCCCXXXVII"));
}
}