Author Topic: Roman numeral converter  (Read 312 times)


  • Basileus
    Head of Staff
  • Posts: 2377
  • Karma: 11
    • View Profile
    • Awards
Roman numeral converter
« on: August 07, 2016, 01:00:49 AM »
I thought this might be of interest: it's code to parse a number written in Roman numerals and convert it to conventional Arabic numerals. It's written in a Microsoft proprietary language called C/AL, based on Pascal - I appreciate that most readers won't be familiar with it, but hopefully it's still readable as pseudo-code.

There are a few quirks:
- C/AL provides a textual data type called Code, which forces any text input into upper case. This neatly enabled me to avoid explicit case conversions or making the code handle both cases.
- C/AL also provides a data type called Option, which I've used for the character types (Unit or Five). It's used for small, fixed lists of values.
- The code provided assumes that the first element in arrays has index 0, as this makes things neater and is true of many common languages.
- I've assumed that the largest valid number to be handled is 3,999 (MMMDCCCLXXXVIII) as I'm not aware of a Roman numeral for 5,000 and I didn't want to handle a special case of MMMM for 4,000.
- I've also assumed that numbers such as 99 and 999 are only validly represented as XCIX and CMXCIX respectively, rather than the possibly valid IC and IM.

Spoiler (click to show/hide)

Readers are welcome to pick this code up and use it, translating to other languages as they wish. Credit and/or a post here to say so would be appreciated but aren't required. I'm reasonably confident that the code correctly handles all valid inputs, and rejects all invalid ones, but I can't guarantee this, and people using it should carry out their own testing.