For a better part of the year, I’ve been working on a Jekyll-based Progressive Web App, BloodBuilder, for managing my Sorcerer in a game of Pathfinder I’ve been playing with some friends. One of the many intricate parts of logic involves the ability to convert Arabic numerals to and from Roman numerals, so I wrote a simple Liquid include to handle this.
You found a Bag of Holding (III)…
In case you aren’t familiar with Roman numerals and how they relate to Arabic numerals, Roman numerals are denoted by letters, each of which represents a numeric value, and by adding each of the values in the sequence together, you arrive at the result.
These seven are the only Roman numerals, and using specific combinations of these numerals in a particular order, from greater to lesser values, you can represent any Arabic numerals; although, the readability of Roman numerals can suffer greatly depending on the number you want to represent.
MDCCCLXXXVIII (1888) is rather unwieldy, but can still be parsed with a consistent set of steps and you’ll quickly arrive at the result.
There are also some exceptions that we need to be aware of based on the left-to-right parsing we’ll be performing on the Roman numerals, so let’s focus for a moment on the first ten Roman numerals and highlight these types of exceptions:
We can read Roman numerals such as VII as 5 + 1 + 1 = 7.
Similarly, VIII can be thought of as 5 + 1 + 1 + 1 = 8.
But to follow that line of thinking and think of 9 as 5 + 1 + 1 + 1 + 1 and write VIIII is not correct.
The rule here is that a character in Roman numerals can have no more than three
modifiers. So, to represent 9 in Roman numerals, we instead refer to the next higher value (I, V, X, L, etc.) and modify that value. In this case, the next higher value from V is X, so we say 10 - 1, or
one before ten, so 9 is written as IX.
An easy way to spot these
exceptions is to look for letters which appear out of order—Roman numerals are written with higher-value letters almost always appearing before lower-value letters, reading from left to right, higher to lower—so any letter which appears before a higher value tells you that it falls under the
one before exception.
IV is not 1 + 5, it is
1 before 5, or -1 + 5 = 4.
XL is not 10 + 50, it is
10 before 50, or -10 + 50 = 40.
Rewriting the first ten numbers to take into account this rule, we now have:
First we need to set up some data to be able to relate Arabic numerals to their Roman counterparts.
You’ll notice that I have included the double-letter combinations which represent the
one-value-less case that we described above. This is important to be able to accurately convert numerals, as the combination of the two characters does not equal the sum of the two characters on their own.
Let’s step through converting an Arabic numeral (e.g. 1569) to Roman numerals (MDLXIX). To do this we have to loop through our numeral conversion data from highest to lowest values. If our Arabic numeral is greater than the Arabic value in the data, we subtract that value from our Arabic numeral, we append the Roman value to our output string (which starts as being empty), and we start looping through the data from the top again. If our Arabic numeral is less than the Arabic value in the data, we continue looping and comparing the data to our Arabic numeral.
Because we’re subtracting values from our Arabic numeral as we loop and convert Arabic values to Roman numerals, we will know when we’re done because our Arabic numeral will equal 0.
The same can be done for going from Roman numerals to Arabic. To do so, we have to check each character in-sequence and tally up their values to arrive at the Arabic value. Like before, we have to watch out for the
one-value-less exceptions, so instead of just checking each single character in sequence, we’ll first check if the next two characters in the Roman numeral sequence match an exception, and if so, use that value instead.
In this case we know we’re done converting when we’ve run out of Roman characters to parse.
You’ll also notice, in both cases, that there’s a loop that goes from 1 through 999. While normally this would be a point of poor performance, this limit should never be reached, unless the value being converted is extremely long, in which case Roman numerals would be poorly-suited to represent. This big loop is used to cycle through an arbitrary number of characters passed as an input to the include.
And there you have it. Should you need a
covers-all-bases solution in Liquid for converting to and from Roman numerals, this will do the trick.