Skip to content

Commit 82be0d7

Browse files
committed
Convert roman to decimal: 2nd commit
1 parent 3e21fe4 commit 82be0d7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

string/RomanToDecimal.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package string;
22

3+
/*******************************************************
4+
* @author SAGAR PAUL (paulsagar1a)
5+
* @category String
6+
*******************************************************/
7+
/*Convert a roman number to a decimal number*/
38
public class RomanToDecimal {
49

10+
//get all the values of each roman number
511
private int getDecimalValue(char ch) {
612
if(ch == 'I')
713
return 1;
@@ -20,17 +26,20 @@ private int getDecimalValue(char ch) {
2026
return -1;
2127
}
2228

29+
//function that converts roman to decimal
2330
private int getDecimal(String roman) {
2431
int decimal = 0;
2532
int length = roman.length();
2633
for(int i=0; i<length; i++) {
2734
int num1 = getDecimalValue(roman.charAt(i));
2835
if(i+1 < length) {
2936
int num2 = getDecimalValue(roman.charAt(i+1));
37+
//if next roman number is greater than previous one
38+
//subtract the previous from the next
3039
if(num1 < num2) {
3140
decimal += num2 - num1;
3241
i++;
33-
} else {
42+
} else { //only add the current number
3443
decimal += num1;
3544
}
3645
} else {

0 commit comments

Comments
 (0)