Home > Java > Roman Format ~ behind the scene

Roman Format ~ behind the scene


Hari ini di kantor saya mendapat suatu yang baru dan seru untuk dibagikan, yaitu bagaimana cara mengubah angka arab menjadi angka romawi. Hal pertama yang saya lakukan adalah googling, siapa tahu di Java sudah tersedia fungsinya. Belum lama berseluncur, saya menemukan blog yang berisi Program Konversi Bilangan Bulat / Desimal ke Romawi, setelah membaca isinya dapat disimpulkan bahwa belum tersedia fungsinya di Java. Akhirnya saya memutuskan untuk membuat fungsi sendiri.

Aturan penulisan romawi saya ambil dari wikipedia, yaitu sebagai berikut:

  • A number written in Arabic numerals can be broken into digits. For example, 1903 is composed of 1, 9, 0, and 3. To write the Roman numeral, each of the non-zero digits should be treated separately. In the above example, 1,000 = M, 900 = CM, and 3 = III. Therefore, 1903 = MCMIII.[6]
  • The symbols “I”, “X”, “C”, and “M” can be repeated three times in succession, but no more. (They may appear more than three times if they appear non-sequentially, such as XXXIX.) “D”, “L”, and “V” can never be repeated.[7][8]
  • “I” can be subtracted from “V” and “X” only. “X” can be subtracted from “L” and “C” only. “C” can be subtracted from “D” and “M” only. “V”, “L”, and “D” can never be subtracted[8]
  • Only one small-value symbol may be subtracted from any large-value symbol.[9]

Berhubung saya membuat untuk CC4J, code yang saya bagikan hanya crucial code nya saja.

    private static void _initRoman() {
        DICT_ROMAN.put("1^0", "I");
        DICT_ROMAN.put("5^0", "V");
        DICT_ROMAN.put("1^1", "X");
        DICT_ROMAN.put("5^1", "L");
        DICT_ROMAN.put("1^2", "C");
        DICT_ROMAN.put("5^2", "D");
        DICT_ROMAN.put("1^3", "M");
    }

    private static String _roman(int num, int pow) {
        Object object = DICT_ROMAN.get(num + "^" + pow);
        return isNull(object) ? "[?]" : object.toString();
    }

    public static String roman(Integer digit) {
        String roman = "";
        String sDigit = getStringOrBlank(digit);
        for (int i = 0; i < sDigit.length(); i++) {
            int num = parseInt(sDigit.substring(i, i + 1));
            int pow = sDigit.length() - i - 1;
            while (num > 0) {
                if (num == 9) {
                    roman += _roman(1, pow) + _roman(1, pow + 1);
                    num -= 9;
                } else if (num >= 5) {
                    roman += _roman(5, pow);
                    num -= 5;
                } else if (num >= 4) {
                    roman += _roman(1, pow) + _roman(5, pow);
                    num -= 4;
                } else {
                    roman += _roman(1, pow);
                    num -= 1;
                }
            }
        }
        return roman;
    }

Semoga membantu..

Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: