How a Roman Numeral Converter Works: Rules, Algorithms and Examples
A dependable Roman numeral converter is a small program with a strict contract. It accepts an integer in a stated range, builds a canonical string with a greedy pass, and accepts a string only when that string is the same one the encoder would have written. This article follows the algorithm used by the converter on Numeral Archive.
The ordered pairs
The encoder keeps one list, from largest value to smallest: 1000 M, 900 CM, 500 D, 400 CD, 100 C, 90 XC, 50 L, 40 XL, 10 X, 9 IX, 5 V, 4 IV, and 1 I. Subtractive pairs sit in that list beside the plain symbols. The program does not keep a private answer for every integer. It builds each answer from the list.
Greedy conversion
The steps are short. Reject anything that is not an integer from 1 through 3999. Start with an empty string and the original number. Walk the pairs. While the remainder is at least the current value, append that symbol and subtract the value. Then move to the next pair. When the remainder is zero, the string is finished.
1987
1987 is at least 1000, so append M and leave 987. 987 can pay for 900, so append CM and leave 87. 87 can pay for 50, so append L and leave 37. 37 pays for 10 three times, so append XXX and leave 7. 7 pays for 5, so append V and leave 2. Two ones append II. The string is MCMLXXXVII. The place view says the same thing in groups: M, CM, LXXX, and VII.
2026
2026 pays for M twice and leaves 26. Nothing in the hundreds list fits under 26. Two tens append XX and leave 6. V and I finish the ones. The result is MMXXVI. A hundreds group of zero is not written as a placeholder.
3999
3999 takes M three times, then CM, then XC, then IX. The result is MMMCMXCIX. A fourth M would be 4000, which this standard encoder refuses. The refusal is a range rule of the tool, not a statement that no later writer ever marked a larger number.
Decoding
Reading runs left to right. Each symbol adds its value unless the next symbol is larger, in which case it subtracts. MCMXCIV is 1000 − 100 + 1000 − 10 + 100 − 1 + 5, which is 1994. The arithmetic is easy to implement and easy to misuse.
Why decoding is not validation
The same arithmetic assigns 49 to IL, because I is smaller than L. IL is still not a canonical numeral. The pattern used here allows only the six subtractive pairs, limits repeats, and requires the groups to stand in order. After a string passes that pattern, the program decodes it and encodes the integer again. The second string must match the normalized input. If it does not, the input is rejected.
What gets rejected
Empty input, a blank that someone might hope means zero, IIII, VV, XXXX, IIV, MCMC, and MMMM all fail the standard check. IL, IC, XD, and XM fail as well. Lowercase Latin letters are uppercased first, and a short list of Unicode Roman characters such as Ⅳ is expanded to Latin letters before the same check. A character that merely looks like M is not removed and is not accepted.
Range and the extended tool
Standard mode stops at 3999. The extended converter is a separate convention: one overline multiplies a canonical group by 1000, up to 3,999,999. It does not add a second overline, and it does not pretend that every historical manuscript used this exact mark. Numbers from 1 through 3999 stay in ordinary letters even if you open the extended tool.
Worked check on 1994
Start from MCMXCIV. M adds 1000. C is smaller than the next M, so it subtracts 100. The second M adds 1000. X is smaller than C, so it subtracts 10. C adds 100. I is smaller than V, so it subtracts 1, and V adds 5. The running total is 1994. Encoding 1994 with the greedy list yields MCMXCIV again. That match is the acceptance test. A string that totals 1994 by some other spelling is not accepted merely because the loose sum is familiar.
Normalization in practice
Surrounding spaces are trimmed. Spaces inside the numeral are an error. The field does not turn a failed parse into zero, and it does not leave yesterday’s result on screen after you type something invalid. That behavior matters more than a decorative result panel. Try 1987, then try IL, and compare the two responses in the validator.
Questions
Could a lookup table replace the loop?
A table could store answers, but this site’s encoder is the loop over the thirteen pairs. The tests re-encode every integer from 1 through 3999 and require a unique round trip.
Does 1.78-style rounding appear?
No. The values are integers. A fraction is not a legal standard input, and a fractional arithmetic result is not quietly replaced by a numeral.
Is 4000 written MMMM?
Not here. Four thousands are outside the standard pattern. The extended form used by this site is IV with an overline.