Skip to content
Michael Lizzio

Roman Numeral Converter

An early two-way Roman numeral converter with its own validity check

  • Python
  • NumPy
  • Command-line interface

Complete

Ancient Roman inscription on a stone slab with the title Roman Numeral Converter
Adapted from a photograph by Sonse, licensed CC BY 2.0.

I wrote this on a Raspberry Pi before I even knew about while loops. It converts Roman numerals into regular numbers, checks whether the input is valid, and uses a separate conversion routine for the other direction. Looking back at it is a fun snapshot of how I solved problems before I knew the normal tools for solving them.

Try the converter

Convert in either direction, then open the trace to see exactly how the answer was built.

Conversion direction

Use I, V, X, L, C, D, and M.

Try an example:

Your result will appear here.

A strange but useful validator

The program decodes the Roman numeral, converts the answer back into a canonical Roman numeral, and compares that result with the original input. If the strings do not match, it can reject forms such as IIII and suggest IV instead.

How the two directions work

Reading a Roman numeral goes from left to right. A symbol is added when it is at least as large as the symbol after it, but subtracted when a larger symbol follows it. That is why VI is 5 + 1, while IV is -1 + 5.

Writing a Roman numeral works in the other direction. The converter repeatedly takes the largest symbol or subtractive pair that fits into the number, appends it, and continues with the remainder. The playable trace above exposes each of those decisions.