This is the code I am using, right next to the excel column name converter. Why isnt there an apache library for this stuff?
private static final char[] R = {'ↂ', 'ↁ', 'M', 'D', 'C', 'L', 'X', 'V', 'I'};
// or, as suggested by Andrei Fierbinteanu
// private static final String[] R = {"X\u0305", "V\u0305", "M", "D", "C", "L", "X", "V", "I"};
private static final int MAX = 10000; // value of R[0], must be a power of 10
private static final int[][] DIGITS = {
{},{0},{0,0},{0,0,0},{0,1},{1},
{1,0},{1,0,0},{1,0,0,0},{0,2}};
public static String int2roman(int number) {
if (number < 0 || number >= MAX*4) throw new IllegalArgumentException(
"int2roman: " + number + " is not between 0 and " + (MAX*4-1));
if (number == 0) return "N";
StringBuilder sb = new StringBuilder();
int i = 0, m = MAX;
while (number > 0) {
int[] d = DIGITS[number / m];
for (int n: d) sb.append(R[i-n]);
number %= m;
m /= 10;
i += 2;
}
return sb.toString();
}
Edit:
Now that I look at it again, the loop can be condensed to
for (int i = 0, m = MAX; m > 0; m /= 10, i += 2) {
int[] d = DIGITS[(number/m)%10];
for (int n: d) sb.append(R[i-n]);
}
making the code even less readable ;-)