Roman To Numbers
This algorithm converts roman numerals to decimal numbers.
private enum RomanDigit
{
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
}
public static int RomanToNumbers(string roman)
{
roman = roman.ToUpper().Trim();
if (roman == "N") return 0;
int ptr = 0;
ArrayList values = new ArrayList();
int maxDigit = 1000;
while (ptr < roman.Length)
{
char numeral = roman[ptr];
int digit = (int)Enum.Parse(typeof(RomanDigit), numeral.ToString());
int nextDigit = 0;
if (ptr < roman.Length - 1)
{
char nextNumeral = roman[ptr + 1];
nextDigit = (int)Enum.Parse(typeof(RomanDigit), nextNumeral.ToString());
if (nextDigit > digit)
{
maxDigit = digit - 1;
digit = nextDigit - digit;
ptr++;
}
}
values.Add(digit);
ptr++;
}
int total = 0;
foreach (int digit in values)
total += digit;
return total;
}
Example
string data = "CDXIX";
int value = RomanToNumbers(data);
Output
419