The question of how to convert the Roman numerals to decimal integers recently came up on the forums.
As, the C# implementations I could find on the internet were either over-complicated or defective in some way, I thought I'd write my own.
I've also written a method to convert in the opposite direction; from decimal integers to the Roman numerals, and the code has been reproduced below in case it's of help to anyone.
Please note that the only Roman numerals, as commonly used today, are dealt with i.e. the symbol set is: I, V, X, L, C, D and M.
I've also followed the convention, that, not more than three symbols of the same kind can be used together. So 'IIII' (for 4) is regarded as invalid and you should use 'IV' instead, which is an example of the, so called, 'subtractive notation'.
It follows from this that only the decimal integers in the range 1 to 3999 can be validly represented as Roman numerals and an attempt to convert the numbers outside this range therefore results in an exception.
- using System;
- using System.Text;
-
- namespace RomanNumerals
- {
- static class Roman
- {
- static string[] roman1 = { "MMM", "MM", "M" };
- static string[] roman2 = { "CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C" };
- static string[] roman3 = { "XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X" };
- static string[] roman4 = { "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I" };
-
- public static bool TryParse(string text, out int value)
- {
- value = 0;
- if (String.IsNullOrEmpty(text)) return false;
- text = text.ToUpper();
- int len = 0;
-
- for (int i = 0; i < 3; i++)
- {
- if (text.StartsWith(roman1[i]))
- {
- value += 1000 * (3 - i);
- len = roman1[i].Length;
- break;
- }
- }
-
- if (len > 0)
- {
- text = text.Substring(len);
- len = 0;
- }
-
- for (int i = 0; i < 9; i++)
- {
- if (text.StartsWith(roman2[i]))
- {
- value += 100 * (9 - i);
- len = roman2[i].Length;
- break;
- }
- }
-
- if (len > 0)
- {
- text = text.Substring(len);
- len = 0;
- }
-
- for (int i = 0; i < 9; i++)
- {
- if (text.StartsWith(roman3[i]))
- {
- value += 10 * (9 - i);
- len = roman3[i].Length;
- break;
- }
- }
-
- if (len > 0)
- {
- text = text.Substring(len);
- len = 0;
- }
-
- for (int i = 0; i < 9; i++)
- {
- if (text.StartsWith(roman4[i]))
- {
- value += 9 - i;
- len = roman4[i].Length;
- break;
- }
- }
-
- if (text.Length > len)
- {
- value = 0;
- return false;
- }
-
- return true;
- }
-
- public static string ToRoman(int num)
- {
- if (num > 3999) throw new ArgumentException("Too big - can't exceed 3999");
- if (num < 1) throw new ArgumentException("Too small - can't be less than 1");
- int thousands, hundreds, tens, units;
- thousands = num / 1000;
- num %= 1000;
- hundreds = num / 100;
- num %= 100;
- tens = num / 10;
- units = num % 10;
- var sb = new StringBuilder();
- if (thousands > 0) sb.Append(roman1[3 - thousands]);
- if (hundreds > 0) sb.Append(roman2[9 - hundreds]);
- if (tens > 0) sb.Append(roman3[9 - tens]);
- if (units > 0) sb.Append(roman4[9 - units]);
- return sb.ToString();
- }
- }
-
- class Program
- {
- static void Main()
- {
- string[] romans = { "I", "MMIV", "DCL", "CCXXII", "IIII", "CZ", "MLXVI", "III", "DCCXIV", "MIM" };
- int value = 0;
-
- foreach (string roman in romans)
- {
- if (Roman.TryParse(roman, out value)) Console.WriteLine("{0} = {1}", roman, value);
- }
-
- Console.WriteLine();
-
- int[] numbers = { 1, 2004, 650, 222, 4, 99, 1066, 3, 714, 1999, 3999 };
-
-
- foreach (int number in numbers)
- {
- Console.WriteLine("{0} = {1}", number, Roman.ToRoman(number));
- }
-
- Console.ReadKey();
- }
-
- }
- }
The output of this program is:
I = 1
MMIV = 2004
DCL = 650
CCXXII = 222
MLXVI = 1066
III = 3
DCCXIV = 714
1 = I
2004 = MMIV
650 = DCL
222 = CCXXII
4 = IV
99 = XCIX
1066 = MLXVI
3 = III
714 = DCCXIV
1999 = MCMXCIX
3999 = MMMCMXCIX
For further details on Roman numerals, check out this Wikipedia article: