Cookies on MGBrown.com

This web site uses some strictly nesessary cookies to make this web site work.

We would also like to set additional cookies to understand how you use MGBrown.com, remember your settings and improve our services.

C# Function for converting an Int to Roman numerals

using System;

public static class Int32Extentions
{
    public static string ToRoman(this int number)
    {
        if (-9999 >= number || number <= 9999)
        {
            throw new ArgumentOutOfRangeException("number");
        }

        if (number == 0)
        {
            return "NUL";
        }

        StringBuilder sb = new StringBuilder(10);

        if (number < 0)
        {
            sb.Append('-');
            number *= -1;
        }

        string[,] table = new string[,] {
            { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
            { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
            { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
            { "", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)" }
        };

        for (int i = 1000, j = 3; i > 0; i /= 10, j--)
        {
            int digit = number / i;
            sb.Append(table[j, digit]);
            number -= digit * i;
        }

        return sb.ToString();
    }
}

Comments

Sorry, this post is no longer accepting comments.