Roman numerals code

ben / tech / roman numerals

Java

Roman.java (5.1k) is a Java class for converting to/from Roman numerals. It is demonstrated in this applet.

Haskell

I also have a Literate Haskell document converting from Roman numerals to integers (but not the other way round) which is available as Hrn.lhs (2.2k). It implements the same algorithm as the above Java code.

The Algorithm

Whilst there are a number of styles for writing Roman numerals (several of which are implemented in the above code), there appears to be a simple algorithm that can correctly parse almost any 'reasonable'-looking Roman number (sufficiently simple that I'm inclined to treat it as a test for whether a number is valid or not). One thing (way down on the todo list) is to gather together examples of the various styles in use (the most bizarre I've seen so far is the (?)Borgias Rooms in the Vatican, though all of these are correctly parsed by my above algorithm, I think).

All this code is licensed under a BSD-like license.