Roman Numeral Conversion in Both Directions
Convert integers from 1 to 3999 into Roman numerals and parse valid numerals back to numbers with subtraction and validation.
Code
const NUMERALS = [
[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"],
[100, "C"], [90, "XC"], [50, "L"], [40, "XL"],
[10, "X"], [9, "IX"], [5, "V"], [4, "IV"], [1, "I"],
];
export function toRoman(value) {
if (!Number.isInteger(value) || value < 1 || value > 3999) {
throw new RangeError("value must be an integer between 1 and 3999");
}
let remaining = value;
let output = "";
for (const [amount, symbol] of NUMERALS) {
while (remaining >= amount) {
output += symbol;
remaining -= amount;
}
}
return output;
}
export function fromRoman(roman) {
if (typeof roman !== "string" || !/^[MDCLXVI]+$/.test(roman)) {
throw new RangeError("roman must contain only MDCLXVI");
}
const values = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };
let total = 0;
for (let index = 0; index < roman.length; index += 1) {
const current = values[roman[index]];
const next = values[roman[index + 1]] ?? 0;
total += current < next ? -current : current;
}
if (total < 1 || total > 3999 || toRoman(total) !== roman) {
throw new RangeError("invalid or non-canonical roman numeral");
}
return total;
}Inside the snippet
Roman Numeral Conversion in Both Directions keeps everything in the functions toRoman and fromRoman.
The core work runs in nested loops — outer for (const [amount, symbol] of NUMERALS), inner while (remaining >= amount). Invalid states are rejected up front: it throws RangeError instead of continuing with bad data.
Putting it to use
The entry-point signature is export function toRoman(value)
toRoman is what another module would consume; nothing else here is load-bearing for callers.
Core lines
throw new RangeError("value must be an integer between 1 and 3999");
throw new RangeError("invalid or non-canonical roman numeral");
throw new RangeError("roman must contain only MDCLXVI");
Interactive Tools for This Topic
Roman Numeral ConverterConvert integers between 1 and 3999 to Roman numerals and back, rejecting malformed numeral forms.A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.Big-O Complexity VisualizerVisualize and compare algorithm time complexities like O(1), O(log n), O(n), O(n log n), and O(n²). Plot growth curves to understand performance scaling at a glance.CSS Unit ConverterConvert a length between px, rem, em, pt, pc, in, cm, mm, and percent using a configurable root font size, following the 96dpi CSS definition.
FAQ
- What happens on invalid input?
- Validation comes first — `RangeError` marks the failure, before any partial work happens.
- Does this require external packages?
- No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. The file defines `toRoman`, `fromRoman` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.