Java Decimal to Roman decimal2roman(int src)

Here you can find the source of decimal2roman(int src)

Description

decimalroman

License

Open Source License

Declaration

public static String decimal2roman(int src) 

Method Source Code

//package com.java2s;
/*/*from  ww  w .  j a va 2s.  c o m*/
 * This file is part of MyPet
 *
 * Copyright ? 2011-2016 Keyle
 * MyPet is licensed under the GNU Lesser General Public License.
 *
 * MyPet 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 3 of the License, or
 * (at your option) any later version.
 *
 * MyPet 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String decimal2roman(int src) {
        char digits[] = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
        String thousands = "", result = "";
        int rang, digit, i;

        for (i = src / 1000; i > 0; i--) {
            thousands += "M";
        }
        src %= 1000;

        rang = 0;
        while (src > 0) {
            digit = src % 10;
            src /= 10;
            switch (digit) {
            case 1:
                result = "" + digits[rang] + result;
                break;
            case 2:
                result = "" + digits[rang] + digits[rang] + result;
                break;
            case 3:
                result = "" + digits[rang] + digits[rang] + digits[rang] + result;
                break;
            case 4:
                result = "" + digits[rang] + digits[rang + 1] + result;
                break;
            case 5:
                result = "" + digits[rang + 1] + result;
                break;
            case 6:
                result = "" + digits[rang + 1] + digits[rang] + result;
                break;
            case 7:
                result = "" + digits[rang + 1] + digits[rang] + digits[rang] + result;
                break;
            case 8:
                result = "" + digits[rang + 1] + digits[rang] + digits[rang] + digits[rang] + result;
                break;
            case 9:
                result = "" + digits[rang] + digits[rang + 2] + result;
                break;
            }
            rang += 2;
        }
        return thousands + result;
    }
}

Related

  1. decimalToRoman(int binary, boolean oldFashion)