Skip to content

Instantly share code, notes, and snippets.

@mlow
Created August 6, 2012 16:04
Show Gist options
  • Select an option

  • Save mlow/3275962 to your computer and use it in GitHub Desktop.

Select an option

Save mlow/3275962 to your computer and use it in GitHub Desktop.
Java from int to roman numeral
private static String[] RVALS = {"M", "CM", "D", "CD", "C", "XC", "L",
"XL", "X", "IX", "V", "IV", "I"};
private static int[] IVALS = {1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1};
public static String toRomanNumeral(int value) {
if (value >= 4000 || value <= 0) return "";
String roman = "";
for (int i = 0; i < RVALS.length; i++) {
while (value >= IVALS[i]) {
roman += RVALS[i];
value -= IVALS[i];
}
}
return roman;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment