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
direction (string, required) one of: to_roman, to_arabic : Which way to convert.
value (string, required) : For to_roman: an integer 1-3999 as a string. For to_arabic: a Roman numeral string.
Outputs
result: The converted value: a Roman numeral string, or an Arabic integer.
Steps
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.
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.
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.
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
A History of Mathematical Notations (Roman numeral subtractive notation) (Open Court, 2026-09-30T19:59:00Z) - Standard reference for the additive/subtractive rules of Roman numeral notation.