public static String GetString(int index)
{
StringBuilder buf = new StringBuilder();
// lower than 0 ? Add minus
if (index < 0) {
buf.Append('-');
index = -index;
}
// greater than 3000
if (index > 3000) {
buf.Append('|');
buf.Append(GetString(index / 1000));
buf.Append('|');
// remainder
index = index - (index / 1000) * 1000;
}
// number between 1 and 3000
int pos = 0;
while (true) {
// loop over the array with values for m-d-c-l-x-v-i
RomanDigit dig = roman[pos];
// adding as many digits as we can
while (index >= dig.value) {
buf.Append(dig.digit);
index -= dig.value;
}
// we have the complete number
if (index <= 0) {
break;
}
// look for the next digit that can be used in a special way
int j = pos;
while (!roman[++j].pre);
// does the special notation apply?
if (index + roman[j].value >= dig.value) {
buf.Append(roman[j].digit).Append(dig.digit);
index -= dig.value - roman[j].value;
}
pos++;
}
return buf.ToString();
}