arabic number to roman number java

Solutions on MaxInterview for arabic number to roman number java by the best coders in the world

showing results for - "arabic number to roman number java"
Clara
15 Jan 2019
1public static String arabicToRoman(int number) {
2    if ((number <= 0) || (number > 4000)) {
3        throw new IllegalArgumentException(number + " is not in range (0,4000]");
4    }
5
6    List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
7
8    int i = 0;
9    StringBuilder sb = new StringBuilder();
10
11    while ((number > 0) && (i < romanNumerals.size())) {
12        RomanNumeral currentSymbol = romanNumerals.get(i);
13        if (currentSymbol.getValue() <= number) {
14            sb.append(currentSymbol.name());
15            number -= currentSymbol.getValue();
16        } else {
17            i++;
18        }
19    }
20
21    return sb.toString();
22}