Advertisement
Guest User

Number to Roman

a guest
Feb 25th, 2014
3,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1.     public static string To(int number)
  2.     {
  3.         var romanNumerals = new string[][]
  4.         {
  5.             new string[]{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, // ones
  6.             new string[]{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, // tens
  7.             new string[]{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, // hundreds
  8.             new string[]{"", "M", "MM", "MMM"} // thousands
  9.         };
  10.  
  11.         // split integer string into array and reverse array
  12.         var intArr = number.ToString().Reverse().ToArray();
  13.         var len = intArr.Length;
  14.         var romanNumeral = "";
  15.         var i = len;
  16.  
  17.         // starting with the highest place (for 3046, it would be the thousands
  18.         // place, or 3), get the roman numeral representation for that place
  19.         // and add it to the final roman numeral string
  20.         while (i-- > 0)
  21.         {
  22.             romanNumeral += romanNumerals[i][Int32.Parse(intArr[i].ToString())];
  23.         }
  24.  
  25.         return romanNumeral;
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement