Method Deck / other

Roman numeral conversion

Converts between Arabic numbers (1-3999) and Roman numerals using standard subtractive notation.

Published for review. The Founder is checking every card himself; anything he strikes comes down the same hour.

Try it

Inputs

Outputs

Steps

  1. Standard subtractive notation: symbols I=1, V=5, X=10, L=50, C=100, D=500, M=1000; a smaller symbol placed immediately before a larger one is subtracted (IV=4, IX=9, XL=40, XC=90, CD=400, CM=900); otherwise symbols are added left to right.
  2. To convert to Roman: repeatedly take the largest value-symbol pair (from 1000 down to 1, including the six subtractive pairs CM, CD, XC, XL, IX, IV) that fits into the remaining number, appending its symbol and subtracting its value, until the remaining number is 0.
  3. To convert to Arabic: scan the numeral left to right, adding each symbol's value, except when a symbol's value is less than the value of the symbol immediately following it, in which case it is subtracted instead.
  4. Valid range for to_roman is 1 to 3999 (the standard scheme has no symbol for zero or for 4000 and above without overline notation).

Constants

None.

Sources

Worked example and test vectors

NameInputsAnswer
worked example{"direction":"to_roman","value":"1994"}MCMXCIV
test vector: to Arabic{"direction":"to_arabic","value":"MMXXVI"}2026

Published card