locked
how to convert a number into roman numeral in visual C# RRS feed

  • General discussion

  • how to convert a number into roman numeral in visual C#
    Tuesday, April 2, 2013 2:36 PM

All replies

  • There's no standard method for this, so you have to write it.

    For example, you can try something like this (simple and recursive):

    public static string ToRoman(int number)
        {
            if ((number < 0) || (number > 3999)) throw new ArgumentOutOfRangeException("Value must be between 1 and 3999");
            if (number < 1) return string.Empty;            
            if (number >= 1000) return "M" + ToRoman(number - 1000);
            if (number >= 900) return "CM" + ToRoman(number - 900); //EDIT: i've typed 400 instead 900
            if (number >= 500) return "D" + ToRoman(number - 500);
            if (number >= 400) return "CD" + ToRoman(number - 400);
            if (number >= 100) return "C" + ToRoman(number - 100);            
            if (number >= 90) return "XC" + ToRoman(number - 90);
            if (number >= 50) return "L" + ToRoman(number - 50);
            if (number >= 40) return "XL" + ToRoman(number - 40);
            if (number >= 10) return "X" + ToRoman(number - 10);
            if (number >= 9) return "IX" + ToRoman(number - 9);
            if (number >= 5) return "V" + ToRoman(number - 5);
            if (number >= 4) return "IV" + ToRoman(number - 4);
            if (number >= 1) return "I" + ToRoman(number - 1);
            throw new ArgumentOutOfRangeException("Value must be between 1 and 3999");
        }

    Tuesday, April 2, 2013 2:46 PM
  • I found a nice list of the Rules regardign it:

    http://projecteuler.net/about=roman_numerals

    One trick I use when having to write down a Number "from biggest to lowest" is to use the Modulo & devision Operators:

    uint input; //Here is teh int you want to transform
    
    //M step
    uint rest = input % 1000;
    WriteM(input/1000);
    input = rest;
    
    //CM step
    if(input % 500 < 0){
      WriteCM();
      input -= 500;
    }
    
    //C Step
    ....
    Modulo allows you to extract the rest for later usage, thus allowing you only focus on that sinlge Number at a time and go from highest to lowest.

    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Tuesday, April 2, 2013 5:59 PM
  • private static string ConvertToRoman(int convertThis)
        {
            int leftovers;              //store mod results
            string RomanNumeral = "";   //store roman numeral string
            Dictionary<string, int> dict = new Dictionary<string, int>()
            {
                {"M", 1000},// 1000 = M
                {"CM", 900},// 900 = CM
                {"D", 500}, // 500 = D
                {"CD", 400},// 400 = CD
                {"C", 100}, // 100 = C
                {"XC", 90}, // 90 = XC
                {"L", 50},  // 50 = L
                {"XL", 40}, // 40 = XL
                {"X", 10},  // 10 = X
                {"IX", 9},  // 9 = IX
                {"V", 5},   // 5 = V
                {"IV", 4},  // 4 = IV
                {"I", 1},   // 1 = I
            };
            foreach (KeyValuePair<string, int> pair in dict)
            {
                if (convertThis >= pair.Value)
                {
                    leftovers = convertThis % pair.Value;
                    int remainder = (convertThis - leftovers) / pair.Value;
                    convertThis = leftovers;
                    while (remainder > 0)
                    {
                        RomanNumeral += pair.Key; remainder--;
                    }
                }
            }
            return RomanNumeral;
        }
    Tuesday, January 7, 2014 1:03 PM
  • Hello,

       Check solution for you.

      
    static string convertroman(int number)
        {
            int l = number / 10;
            StringBuilder sb = new StringBuilder();
            for (int m = 0; m <= l; m++)
            {
                if (l == 0)
                {
                    break;
                }
                if (l == 5)
                {
                    sb = sb.Append(ro.L.ToString());
                    break;
                }
                if (l == 4)
                {
                    sb = sb.Append(ro.X.ToString()).Append(ro.L.ToString());
                    break;
                }
                if (l == 9)
                {
                    sb = sb.Append(ro.X.ToString()).Append(ro.C.ToString());
                    break;
                }
                if (l == 10)
                {
                    sb = sb.Append(ro.C.ToString());
                    break;
                }
    
                if (l > 5 && l < 9)
                {
                    sb = sb.Append(ro.L.ToString());
                    l = l - 5;
                    m = 0;
                    // break;
                    continue;
                }
                if (l > 10)
                {
                    sb = sb.Append(ro.C.ToString());
                    l = l - 10;
                    m = 0;
                    // continue;
    
                }
                else
                {
                    sb = sb.Append(ro.X.ToString());
                }
    
            }
            int z = number % 10;
            for (int x = 0; x <= z; x++)
            {
                if (z == 0)
                {
                    break;
                }
                if (z == 5)
                {
                    sb = sb.Append(ro.V.ToString());
                    break;
                }
                if (z == 4)
                {
                    sb = sb.Append(ro.I.ToString()).Append(ro.V.ToString());
                    break;
                }
                if (z == 9)
                {
                    sb = sb.Append(ro.I.ToString()).Append(ro.X.ToString());
                    break;
                }
                if (z == 10)
                {
                    sb = sb.Append(ro.X.ToString());
                    break;
                }
                if (z > 5 && z < 9)
                {
                    sb = sb.Append(ro.V.ToString());
                    z = z - 5;
                    x = 0;
                }
                else
                {
                    sb.Append(ro.I.ToString());
                }              
    
            }
            return sb.ToString();           
        }
    -----------------------------------------
     Mark as answer if it helps to you
     Free .NET Barcode Generator & Scanner supporting over 40 kinds of 1D & 2D symbologies.
    Thursday, January 9, 2014 10:38 AM