java convert to roman numerals

Solutions on MaxInterview for java convert to roman numerals by the best coders in the world

showing results for - "java convert to roman numerals"
Neele
26 Jan 2017
1import java.util.TreeMap;
2
3public class RomanNumber {
4
5    private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>();
6
7    static {
8
9        map.put(1000, "M");
10        map.put(900, "CM");
11        map.put(500, "D");
12        map.put(400, "CD");
13        map.put(100, "C");
14        map.put(90, "XC");
15        map.put(50, "L");
16        map.put(40, "XL");
17        map.put(10, "X");
18        map.put(9, "IX");
19        map.put(5, "V");
20        map.put(4, "IV");
21        map.put(1, "I");
22
23    }
24
25    public final static String toRoman(int number) {
26        int l =  map.floorKey(number);
27        if ( number == l ) {
28            return map.get(number);
29        }
30        return map.get(l) + toRoman(number-l);
31    }
32
33}
34