How to get roman numeral from number

Discussion in 'Plugin Development' started by william9518, Jan 11, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    william9518

    like I would want a method like

    public String getRomanNumeralFromNumber(int number){
    return romannumeral of number!@@;
    }
    how can I get a number to its roman numeral counterpart?
     
  2. Offline

    ZeusAllMighty11

    I don't know if Java knows roman numerals automatically, but it can't be too difficult to set up your own system.. actually, it might be if you're not too advanced.


    Because acknowledge basic like :
    I = 1
    V = 5
    X = 10

    But you can't just recognize, for example '12' as IIIIIIIIIIII but as XII... It may also be confusing when you need to remember that numbers less than the higher one, ehhh like '4' has a part before and after 'IV', where subtraction is..



    I'll see what I can find on roman numerals


    Edit: http://www.roseindia.net/java/java-tips/45examples/misc/roman/roman.shtml
     
  3. Offline

    Ne0nx3r0

    Here's a util class I've been using:
    https://github.com/Ne0nx3r0/RareItems2/blob/master/com/gmail/ne0nx3r0/utils/RomanNumeral.java

    Code:
    package com.gmail.ne0nx3r0.utils;
     
    public class RomanNumeral {
        final static char symbol[] = {'M','D','C','L','X','V','I'};
        final static int  value[] = {1000,500,100,50,10,5,1};
     
        public static int valueOf(String roman)
        {
            roman = roman.toUpperCase();
            if(roman.length() == 0)
                    {
                        return 0;
                    }
            for(int i = 0; i < symbol.length; i++)
            {
                int pos = roman.indexOf(symbol[i]) ;
                if(pos >= 0)
                            {
                                return value[i] - valueOf(roman.substring(0,pos)) + valueOf(roman.substring(pos+1));
                            }
            }
            throw new IllegalArgumentException("Invalid Roman Symbol.");
        }
     
        private static int[]    numbers = { 1000,  900,  500,  400,  100,  90, 
            50,  40,  10,    9,    5,    4,    1 };
     
        private static String[] letters = { "M",  "CM",  "D",  "CD", "C",  "XC",
            "L",  "XL",  "X",  "IX", "V",  "IV", "I" };
     
     
        public static String convertToRoman(int N)
        {
            String roman = "";
            for (int i = 0; i < numbers.length; i++) {
                while (N >= numbers[i]) {
                    roman += letters[i];
                    N -= numbers[i];
                }
            }
            return roman;
        }
    }
    /** end of http://code.activestate.com/recipes/577313/ }}} */
    
     
Thread Status:
Not open for further replies.

Share This Page