File tree 1 file changed +10
-1
lines changed
1 file changed +10
-1
lines changed Original file line number Diff line number Diff line change 1
1
package string ;
2
2
3
+ /*******************************************************
4
+ * @author SAGAR PAUL (paulsagar1a)
5
+ * @category String
6
+ *******************************************************/
7
+ /*Convert a roman number to a decimal number*/
3
8
public class RomanToDecimal {
4
9
10
+ //get all the values of each roman number
5
11
private int getDecimalValue (char ch ) {
6
12
if (ch == 'I' )
7
13
return 1 ;
@@ -20,17 +26,20 @@ private int getDecimalValue(char ch) {
20
26
return -1 ;
21
27
}
22
28
29
+ //function that converts roman to decimal
23
30
private int getDecimal (String roman ) {
24
31
int decimal = 0 ;
25
32
int length = roman .length ();
26
33
for (int i =0 ; i <length ; i ++) {
27
34
int num1 = getDecimalValue (roman .charAt (i ));
28
35
if (i +1 < length ) {
29
36
int num2 = getDecimalValue (roman .charAt (i +1 ));
37
+ //if next roman number is greater than previous one
38
+ //subtract the previous from the next
30
39
if (num1 < num2 ) {
31
40
decimal += num2 - num1 ;
32
41
i ++;
33
- } else {
42
+ } else { //only add the current number
34
43
decimal += num1 ;
35
44
}
36
45
} else {
You can’t perform that action at this time.
0 commit comments