Java Decimal to Roman decimalToRoman(int binary, boolean oldFashion)

Here you can find the source of decimalToRoman(int binary, boolean oldFashion)

Description

decimal To Roman

License

Open Source License

Declaration

public static String decimalToRoman(int binary, boolean oldFashion) 

Method Source Code

//package com.java2s;
/*/*  w  w w . j a  v  a 2s .  co  m*/
 * Metadata Editor
 * @author Jiri Kremser
 * 
 * 
 * 
 * Metadata Editor - Rich internet application for editing metadata.
 * Copyright (C) 2011  Jiri Kremser (kremser@mzk.cz)
 * Moravian Library in Brno
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * 
 */

public class Main {
    private static final String[] RCODE_1 = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV",
            "I" };
    private static final String[] RCODE_2 = { "M", "DCCCC", "D", "CCCC", "C", "LXXXX", "L", "XXXX", "X", "VIIII",
            "V", "IIII", "I" };
    private static final int[] BVAL = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

    public static String decimalToRoman(int binary, boolean oldFashion) {
        if (binary <= 0 || binary >= 4000) {
            throw new NumberFormatException("Value outside roman numeral range.");
        }
        String roman = "";

        // Loop from biggest value to smallest, successively subtracting,
        // from the binary value while adding to the roman representation.
        for (int i = 0; i < RCODE_1.length; i++) {
            while (binary >= BVAL[i]) {
                binary -= BVAL[i];
                roman += oldFashion ? RCODE_2[i] : RCODE_1[i];
            }
        }
        return roman;
    }
}

Related

  1. decimal2roman(int src)