Java Integer to Roman integerToRoman(int n)

Here you can find the source of integerToRoman(int n)

Description

Convert integer to a Roman numeral.

License

Open Source License

Parameter

Parameter Description
n The integer to convert to a Roman numeral.

Return

The corresponding Roman numeral as a string.

Declaration


protected static String integerToRoman(int n) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /**   Roman numeral strings. */

    protected static String[] romanNumerals = new String[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX",
            "V", "IV", "I" };
    /**   Integer values for entries in "romanNumerals". */

    protected static int[] integerValues = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

    /**   Convert integer to a Roman numeral.
     */*from  w w  w .  jav  a 2s .c  o m*/
     *   @param   n   The integer to convert to a Roman numeral.
     *
     *   @return      The corresponding Roman numeral as a string.
     */

    protected static String integerToRoman(int n) {
        StringBuffer result = new StringBuffer();

        int i = 0;
        //   Compute Roman numeral string using
        //   successive substraction of
        //   breakpoint values for Roman numerals.
        while (n > 0) {
            while (n >= integerValues[i]) {
                result.append(romanNumerals[i]);
                n -= integerValues[i];
            }

            i++;
        }

        return result.toString();
    }
}

Related

  1. IntegerToRoman(int n)