Created
August 6, 2012 16:04
-
-
Save mlow/3275962 to your computer and use it in GitHub Desktop.
Java from int to roman numeral
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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