I'm trying to write a function that converts numbers to roman numerals. This is my code so far; however, it only works with numbers that are less than 400. Is there a quick and easy way to do this conversion, or extend my existing code so that it handles all cases? Thanks in advance for any help.
static string convertroman(int number)
{
int l = number / 10;
StringBuilder sb = new StringBuilder();
for (int m = 0; m <= l; m++)
{
if (l == 0)
{
break;
}
if (l == 5)
{
sb = sb.Append(ro.L.ToString());
break;
}
if (l == 4)
{
sb = sb.Append(ro.X.ToString()).Append(ro.L.ToString());
break;
}
if (l == 9)
{
sb = sb.Append(ro.X.ToString()).Append(ro.C.ToString());
break;
}
if (l == 10)
{
sb = sb.Append(ro.C.ToString());
break;
}
if (l > 5 && l < 9)
{
sb = sb.Append(ro.L.ToString());
l = l - 5;
m = 0;
// break;
continue;
}
if (l > 10)
{
sb = sb.Append(ro.C.ToString());
l = l - 10;
m = 0;
// continue;
}
else
{
sb = sb.Append(ro.X.ToString());
}
}
int z = number % 10;
for (int x = 0; x <= z; x++)
{
if (z == 0)
{
break;
}
if (z == 5)
{
sb = sb.Append(ro.V.ToString());
break;
}
if (z == 4)
{
sb = sb.Append(ro.I.ToString()).Append(ro.V.ToString());
break;
}
if (z == 9)
{
sb = sb.Append(ro.I.ToString()).Append(ro.X.ToString());
break;
}
if (z == 10)
{
sb = sb.Append(ro.X.ToString());
break;
}
if (z > 5 && z < 9)
{
sb = sb.Append(ro.V.ToString());
z = z - 5;
x = 0;
}
else
{
sb.Append(ro.I.ToString());
}
}
return sb.ToString();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…