Convert Roman Numerals into Numbers
In an earlier article we converted integer values into Roman numerals, an ancient numbering system that uses letters to represent values. In this article we will reverse the process, allowing Roman numerals to be evaluated as an integer.
Roman Numerals System
The Roman numerals system is described in the earlier article describing how to convert an integer value into Roman numerals. I won't repeat the details in this article, except for the rules for combining the letters of the Roman numerals, as these are required for the algorithm. For easy reference purposes, the Roman numeral letters are as follows:
Using combinations of these letters within several defined rules allows the representation of numbers. The rules are described in the following sections.
Rule 1 - Repetition
A single letter may be repeated up to three times consecutively with each occurrence of the value being additive. This means that I is one, II means two and III is three. However, IIII is incorrect for four.
Rule 2 - Additive Combination
Larger numerals must be placed to the left of the smaller numerals to continue the additive combination. So VI equals six and MDCLXI is 1,661.
Rule 3 - Subtractive Combination
A small-value numeral may be placed to the left of a larger value. Where this occurs, for example IX, the smaller numeral is subtracted from the larger. This means that IX is nine and IV is four. The subtracted digit must be at least one tenth of the value of the larger numeral and must be either I, X or C. Accordingly, ninety-nine is not IC but rather XCIX. The XC part represents ninety and the IX adds the nine. In addition, once a value has been subtracted from another, no further numeral or pair may match or exceed the subtracted value. This disallows values such as MCMD or CMC.
Rule 4 - Repeated Use of V, L and D
The numerals that represent numbers beginning with a '5' (V, L and D) may only appear once in each Roman numeral. This rule permits XVI but not VIV.
Rule 5 - Reducing Values
The fourth rule compares the size of value of each the numeral as read from left to right. The value must never increase from one letter to the next. Where there is a subtractive numeral, this rule applies to the combined value of the two numerals involved in the subtraction when compared to the previous letter. This means that XIX is acceptable but XIM and IIV are not.
Rule 6 - Multiplication
To represent numbers of four thousand or greater, lines are added to each letter. For example, a line above a letter multiplies its value by one thousand. To represent 15,015 the Roman numerals are VVVVVV. This rule is not implemented in the algorithm so the code is limited to values up to but not including four thousand.
Rule 7 - Zero
There is very little information that suggests that the system originally had a notation for zero. However, the letter N has been used to represent zero in a text from around 725AD. This will be used in the algorithm.
The Algorithm and the Code
The algorithm and the code for converting a well-formed Roman numeral to an integer is actually very simple to implement. The difficulty of this algorithm is testing that the Roman numeral is indeed well formed and therefore breaks none of the rules described above. The code provided below performs the all of the validation checks before combining the numerals into the final result.
Depending upon how you wish to use this algorithm, you may want to create it in a new class, incorporate it in an existing class or simply hide it behind a form event. To keep the code simple and short, this article assumes you have a class of some kind prepared and are adding a new method to that class. The code simply declares a new public method that accepts the string to be converted as its only parameter.
To prepare, create or identify the class that you will add the method to, then add the following declaration code:
// Converts a Roman numerals value into an integer
public int RomanToNumber(string roman)
NB: If you are creating or adding to a utilities class, you may wish to adjust the declaration to make the method as static.
Holding the Numeral Values
Each of the seven numerals must be associated with its integer equivalent value. As each letter can be thought of as a named integer value, an enumeration is an effective way of declaring the values. Each letter can be named and an integer value associated with it directly by adding the following code to the class.
private enum RomanDigit
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
NB: Remember to mark the enumeration as static if you have created a static method for the RomanToNumber method.
22 January 2008