Die folgende Methode habe ich beim c# Seminar in Hamburg erstellt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private string roman(int number) { StringBuilder result = new StringBuilder(); int[] digitsValues = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 }; string[] romanDigits = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; while (number > 0) { for (int i = digitsValues.Count() - 1; i >= 0; i--) if (number / digitsValues[i] >= 1) { number -= digitsValues[i]; result.Append(romanDigits[i]); break; } } return result.ToString(); } |