Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string To(int number)
- {
- var romanNumerals = new string[][]
- {
- new string[]{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, // ones
- new string[]{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, // tens
- new string[]{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, // hundreds
- new string[]{"", "M", "MM", "MMM"} // thousands
- };
- // split integer string into array and reverse array
- var intArr = number.ToString().Reverse().ToArray();
- var len = intArr.Length;
- var romanNumeral = "";
- var i = len;
- // starting with the highest place (for 3046, it would be the thousands
- // place, or 3), get the roman numeral representation for that place
- // and add it to the final roman numeral string
- while (i-- > 0)
- {
- romanNumeral += romanNumerals[i][Int32.Parse(intArr[i].ToString())];
- }
- return romanNumeral;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement