Contains extension methods for changing a number to Roman representation (ToRoman) and from Roman representation back to the number (FromRoman)
public static class RomanNumeralExtensionsInheritance System.Object → RomanNumeralExtensions
Converts a Roman numeral character span to its integer representation.
public static int FromRoman(System.ReadOnlySpan<char> input);input System.ReadOnlySpan<System.Char>
The Roman numeral character span to convert. Must not be empty.
System.Int32
The integer value represented by the Roman numeral.
System.ArgumentException
Thrown when input is empty (after trimming) or contains an invalid Roman numeral format.
"XIV".AsSpan().FromRoman() => 14
"MCMXC".AsSpan().FromRoman() => 1990This is a memory-efficient overload that works with character spans to avoid string allocations. Valid Roman numerals use the characters M, D, C, L, X, V, and I (case-insensitive). Supports subtractive notation (e.g., IV = 4, IX = 9).
Converts a Roman numeral string to its integer representation.
public static int FromRoman(this string input);input System.String
The Roman numeral string to convert (e.g., "XIV", "MCMXC"). Must not be null.
System.Int32
The integer value represented by the Roman numeral.
System.ArgumentNullException
Thrown when input is null.
System.ArgumentException
Thrown when input is empty or contains an invalid Roman numeral format.
"XIV".FromRoman() => 14
"MCMXC".FromRoman() => 1990
"IV".FromRoman() => 4
"MMXXIII".FromRoman() => 2023Valid Roman numerals use the characters M, D, C, L, X, V, and I (case-insensitive). Supports subtractive notation (e.g., IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, CM = 900). Valid range is 1 to 3999.
Converts an integer to its Roman numeral representation.
public static string ToRoman(this int input);input System.Int32
The integer value to convert. Must be between 1 and 3999 inclusive.
System.String
A string containing the Roman numeral representation of the input value.
System.ArgumentOutOfRangeException
Thrown when input is less than 1 or greater than 3999.
Roman numerals are traditionally limited to this range.
14.ToRoman() => "XIV"
1990.ToRoman() => "MCMXC"
4.ToRoman() => "IV"
2023.ToRoman() => "MMXXIII"
3999.ToRoman() => "MMMCMXCIX"Uses standard Roman numeral notation including subtractive notation for 4, 9, 40, 90, 400, and 900. The implementation is optimized for performance and avoids string allocations where possible.