toRoman
A minimalist TypeScript library for Roman numeral operations — convert, validate, and compute with the ancient numeral system.
npm install toroman
<script src="https://unpkg.com/toRoman"></script>
Quick Start
Import the library and start converting in seconds.
const roman = require("toRoman");
// Convert number → Roman
roman.toRoman(2025); // "MMXXV"
// Convert Roman → number
roman.fromRoman("XLII"); // 42
// Validate a Roman numeral
roman.isRoman("XIV"); // true
// Arithmetic
roman.addRoman("X", "V"); // "XV"
toRoman()
MethodConverts an Arabic integer (1–3999) into its Roman numeral string representation.
toRoman(value: number): string
valuenumberInteger between 1 and 3999
roman.toRoman(765); // "DCCLXV"
roman.toRoman(1994); // "MCMXCIV"
roman.toRoman(4); // "IV"
fromRoman()
MethodParses a Roman numeral string and returns its integer value.
fromRoman(value: string): number
valuestringA valid Roman numeral string
roman.fromRoman("DCCLXV"); // 765
roman.fromRoman("MCMXCIV"); // 1994
roman.fromRoman("IV"); // 4
isRoman()
MethodValidates whether a given string is a correctly formed Roman numeral.
isRoman(value: string): boolean
roman.isRoman("XIV"); // true
roman.isRoman("IIII"); // false
roman.isRoman("hello"); // false
addRoman()
MethodAdds two Roman numeral strings and returns the result as a Roman numeral.
addRoman(a: string, b: string): string
roman.addRoman("X", "V"); // "XV"
roman.addRoman("L", "XXX"); // "LXXX"
subtractRoman()
MethodSubtracts the second Roman numeral from the first.
subtractRoman(a: string, b: string): string
roman.subtractRoman("XX", "V"); // "XV"
roman.subtractRoman("C", "L"); // "L"
multiplyRoman()
MethodMultiplies two Roman numerals and returns the Roman numeral result.
multiplyRoman(a: string, b: string): string
roman.multiplyRoman("X", "III"); // "XXX"
roman.multiplyRoman("V", "IV"); // "XX"
divideRoman()
MethodDivides the first Roman numeral by the second (integer division).
divideRoman(a: string, b: string): string
roman.divideRoman("X", "II"); // "V"
roman.divideRoman("C", "IV"); // "XXV"
rangeRoman()
MethodReturns an array of Roman numerals within the given integer range.
rangeRoman(start: number, end: number): string[]
roman.rangeRoman(1, 5);
// ["I", "II", "III", "IV", "V"]
randomRoman()
MethodGenerates a random Roman numeral between 1 and 3999.
randomRoman(): string
roman.randomRoman(); // e.g. "DCCCXLVII"
tableRoman()
MethodGenerates a lookup table of Roman numerals up to a given limit.
tableRoman(limit: number): Record<number, string>
roman.tableRoman(5);
// { 1: "I", 2: "II", 3: "III", 4: "IV", 5: "V" }
Live Sandbox
Write any JavaScript using the roman object. Results appear instantly.
What's Next
The library is actively evolving. Here's what's planned for the next major release.
Expression Parser
Evaluate Roman numeral expressions directly.
roman.evaluate("X + V * II")
// → "XX"
Immutable Roman Object
A chainable, immutable Roman numeral class.
const x = Roman("X");
x.add("V").toString(); // "XV"
Framework Hooks
First-class React & Vue integration.
const { roman, error }
= useRoman(input);
// React / Vue hook
CLI Improvements
Powerful flags and piping support.
# Convert from Roman
toroman --from XLII
# Pipe input
echo 42 | toroman
Consistent Error Design
Custom error classes with uniform messages.
throw new RomanError(
"Invalid numeral: IIII"
);
Benchmarks
Performance comparisons with similar libraries.
// Benchmark suite
bench("toRoman", () => {
roman.toRoman(3999);
});