Help with Decimal to Roman Numeral

I have to convert a roman numeral to decimal and vise versa. The user enters two roman numerals in a form and adds them.

If they hit a button 'to decimal' it converts the text boxes to decimal.

When I use the button 'to roman' it isn't properly converting them back to roman numerals. This is what I have, I found it online and it isn't working. I know there is problems but I'm not sure where they are.

Instead of converting 10 to x, it converts it to 1. However many actual character the roman numeral's length is.

public string SetString(int intNum) { int m, d, c, l, x, v, i, n; string roman;

 m = intNum / 1000; d = ((intNum % 1000) / 500); c = ((intNum % 500) / 100); l = ((intNum % 100) / 50); x = ((intNum % 50) / 10); v = ((intNum % 10) / 5); i = (intNum % 5); n = m + d + c + l + x + v + i; roman = Convert.ToString(n); while (n > 0) { if (m > 0) { Console.Write("M"); m--; n--; } else if (d > 0) { Console.Write("D"); d--; n--; } else if (c > 0) { Console.Write("C"); c--; n--; } else if (l > 0) { Console.Write("L"); l--; n--; } else if (x > 0) { Console.Write("X"); x--; n--; } else if (v > 0) { Console.Write("V"); v--; n--; } else if (i > 0) { Console.Write("I"); i--; n--; } } return roman; } 

by spencerknows via /r/csharp

Leave a Reply