Mar 052018

ADM

How to convert a roman number in decimal in Java

  • 05 March 2018
  • ADM

 

 

 

How to convert a roman number in decimal in Java - images/logos/java.jpg

 

This article will present how to convert a Roman number into a decimal using Java.

Theory

Symbol I V X L C D M
Value 1 5 10 50 100 500 1000

The numbers between 1 and 10 are composed by a combination between I, V and X. Note that number 4 and 9 are obtained by using the subtractive notation.

Symbol I II III IV V VI VII VIII IX X
Value 1 2 3 4 5 6 7 8 9 10

The same logic is applied fur values between 10 and 100.

Symbol X XX XX XL L LX LXX LXXX XC C
Value 10 20 30 40 50 60 70 80 90 100

Note that 40 (XL) and 90 (XC) follow the same subtractive pattern as 4 and 9.

Now, applying the logic for numbers between 100 and 1000 will get:

Symbol C CC C CD D DC DCC DCCC CM M
Value 100 200 300 400 500 600 700 800 900 1000

Again, note that 400 (CD) and 900 (CM) follow the same subtractive pattern as 4 and 9.

Examples

MCMLXXXII = 1982

MMXVII = 2017

Code

package com.admfactory;

public class RomanNumberToDecimal {

    public static int toDecimal(String str) {
	int len = str.length();

	/**
	 * adding an random char just to be used by the next char to eliminated
	 * unnecessary out of index checks
	 */
	str = str + " ";
	int result = 0;
	for (int i = 0; i < len; i++) {
	    char ch = str.charAt(i);
	    /** the next char is needed in case the number is using subtractive pattern */
	    char nextChar = str.charAt(i + 1);

	    /** if ch is M add 1000 - definitely not part of subtractive pattern */
	    if (ch == 'M') {
		result += 1000;
	    /** if ch is C - possible part of subtractive pattern */
	    } else if (ch == 'C') {
		if (nextChar == 'M') {
		    result += 900;
		    /** Additionally increasing the index by 1 as the subtractive pattern was used */
		    i++;
		} else if (nextChar == 'D') {
		    result += 400;
		    /** Additionally increasing the index by 1 as the subtractive pattern was used */
		    i++;
		} else {
		    result += 100;
		}
	    /** if ch is D add 500 - definitely not part of subtractive pattern */
	    } else if (ch == 'D') {
		result += 500;
	    /** if ch is X - possible part of subtractive pattern */
	    } else if (ch == 'X') {
		if (nextChar == 'C') {
		    result += 90;
		    /** Additionally increasing the index by 1 as the subtractive pattern was used */
		    i++;
		} else if (nextChar == 'L') {
		    result += 40;
		    /** Additionally increasing the index by 1 as the subtractive pattern was used */
		    i++;
		} else {
		    result += 10;
		}
	    /** if ch is L add 50 - definitely not part of subtractive pattern */
	    } else if (ch == 'L') {
		result += 50;
	    /** if ch is V add 5 - definitely not part of subtractive pattern */		
	    } else if (ch == 'V') {
		result += 5;
            /** if ch is I - possible part of subtractive pattern */
	    } else if (ch == 'I') {
		if (nextChar == 'X') {
		    result += 9;
		    /** Additionally increasing the index by 1 as the subtractive pattern was used */
		    i++;
		} else if (nextChar == 'V') {
		    result += 4;
		    /** Additionally increasing the index by 1 as the subtractive pattern was used */
		    i++;
		} else {
		    result += 1;
		}	    
	    }
	}
	return result;
    }

    public static void main(String[] args) {
	System.out.println("Roman number to decimal conversion example");
	System.out.println();
	String rn1 = "MCMLXXXII";
	int dec1 = toDecimal(rn1);
	System.out.println(rn1 + " converts to " + dec1);

	String rn2 = "MMXVII";
	int dec2 = toDecimal(rn2);
	System.out.println(rn2 + " converts to " + dec2);
    }
}

Output

Roman number to decimal example

MCMLXXXII converts to 1982
MMXVII converts to 2017

 

 

 

References