-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathRomanNumberConversion.java
More file actions
109 lines (97 loc) · 2.85 KB
/
RomanNumberConversion.java
File metadata and controls
109 lines (97 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package romannumberconversion;
/**
*
* @author roberta
*/
public class RomanNumberConversion {
private String romanNumber;
public static String decToRoman(int dec) {
if (dec <= 0) {
return " There isn't zero in Roman number System";
}
StringBuilder resul = new StringBuilder();
while (dec >= 1000) {
resul.append("M");
dec -= 1000;
}
while (dec >= 900) {
resul.append("CM");
dec -= 900;
}
while (dec >= 500) {
resul.append("D");
dec -= 500;
}
while (dec >= 400) {
resul.append("CD");
dec -= 400;
}
while (dec >= 100) {
resul.append("C");
dec -= 100;
}
while (dec >= 90) {
resul.append("XC");
dec -= 90;
}
while (dec >= 50) {
resul.append("L");
dec -= 50;
}
while (dec >= 40) {
resul.append("XL");
dec -= 40;
}
while (dec >= 10) {
resul.append("X");
dec -= 10;
}
while (dec >= 9) {
resul.append("IX");
dec -= 9;
}
while (dec >= 5) {
resul.append("V");
dec -= 5;
}
while (dec >= 4) {
resul.append("IV");
dec -= 4;
}
while (dec >= 1) {
resul.append("I");
dec -= 1;
}
return resul.toString();
}
// Returns a string containing the entered Roman numeral in numeral form.
public String romanToDec(String s) {
int resul = 0;
int last_digit = 0;
int current_digit = 0;
for (int i = 0; i < s.length(); i++) {
current_digit = RomanNumber.parse(s.substring(i, i + 1));
//This is the tricky part.
//If the last number is smaller than the curren number, subtract the last number from the current number
//Otherwise, just add the current number. We must also skip the first number from this rule simply because
//e.g. someone enters 1799 in which case it would subtract 1 from 7
if (last_digit < current_digit && last_digit != 0) {
current_digit -= last_digit;
resul -= last_digit;
resul += current_digit;
last_digit = current_digit;
current_digit = 0;
} else {
last_digit = current_digit;
resul += current_digit;
current_digit = 0;
}
}
return String.valueOf(resul);
}
}