
.NET 1.1+Convert a Number into Roman Numerals
Roman numerals are an ancient number system but still have uses in the modern world. Roman numerals are used to represent dates, adorn clock faces and for indexing. This article describes an algorithm and C# code to convert a number into Roman numerals.
Roman Numerals System
Although the Roman numerals system is very old, it still does appear in some circumstances. In filmmaking, it is usual to use the Roman equivalent of the year of production for the final copyright notice. In publishing, contents page and index numbering is often presented in this manner.
The Roman system used a series of letters to represent numbers. The key letters were as follows:
| Letter | Value | Roman |
|---|
| I | one | unus |
| V | five | quique |
| X | ten | decem |
| L | fifty | quinquaginta |
| C | one hundred | centum |
| D | five hundred | quingenti |
| M | one thousand | mille |
Combining Numerals
The numerals can be combined to create all of the numbers between one and four thousand using a set of simple rules. The first rule is that the letters can be repeated several times, with the values of each being additive. This means that I is one, II means two and III is three. However, in the modern representations IIII is not correct for four, as we will see in the third rule.
NB: IIII is used on a clock face. Various reasons are given for this including the aesthetics of the numeral and to give some symmetry of the clock.
The second rule states that the 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.
The third rule allows for a small-value numeral to 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. This rule cannot be used universally, however. The subtracted digit must be at least one tenth of the value of the larger numeral. Accordingly, ninety-nine is not IC but rather XCIX. The XC part represents ninety and the IX adds the nine.
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.
Finally, 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.
Algorithm Goals
The algorithm converts an integer value into Roman numerals using a recursive process. The resultant value is returned as a string to the calling function. To remove the problems of displaying Roman numerals of four thousand or greater, a restriction to values between zero and 3,999 is enforced. This should be a large enough range for most applications.
The Algorithm and the Code
Preparation
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 integer 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 an integer value into Roman numerals
public string NumberToRoman(int number)
{
}
NB: If you are creating or adding to a utilities class, you may wish to adjust the declaration to make the method static. If you do, all of the other declarations detailed below must also be marked as static.
4 October 2007