十进制与罗马数字互转

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.HashMap;
import java.util.Map;

/**
* 罗马数字与十进制计算工具
*
* @author maxzhao
* @since 2022-12-19 09:29
*/
public class RomanSerialNumberUtil {
/**
* 罗马数字对应的十进制
*/
private static final Map<String, Integer> ROMAN_SERIAL_NUMBER_MAP;
/**
* 数字间隔计算,从大到小
*/
private static final int[] DIGITS = new int[13];
/**
* 数字对应小写的罗马数字
*/
private static final Map<Integer, String> NUM_TO_LOWER_ROMAN_SERIAL_NUMBER = new HashMap<>();
/**
* 数字对应大写的罗马数字
*/
private static final Map<Integer, String> NUM_TO_UPPER_ROMAN_SERIAL_NUMBER = new HashMap<>();

static {
ROMAN_SERIAL_NUMBER_MAP = new HashMap<>(32);
ROMAN_SERIAL_NUMBER_MAP.put("I", 1);
ROMAN_SERIAL_NUMBER_MAP.put("i", 1);
ROMAN_SERIAL_NUMBER_MAP.put("V", 5);
ROMAN_SERIAL_NUMBER_MAP.put("v", 5);
ROMAN_SERIAL_NUMBER_MAP.put("X", 10);
ROMAN_SERIAL_NUMBER_MAP.put("x", 10);
ROMAN_SERIAL_NUMBER_MAP.put("L", 50);
ROMAN_SERIAL_NUMBER_MAP.put("l", 50);
ROMAN_SERIAL_NUMBER_MAP.put("C", 100);
ROMAN_SERIAL_NUMBER_MAP.put("c", 100);
ROMAN_SERIAL_NUMBER_MAP.put("D", 500);
ROMAN_SERIAL_NUMBER_MAP.put("d", 500);
ROMAN_SERIAL_NUMBER_MAP.put("M", 1000);
ROMAN_SERIAL_NUMBER_MAP.put("m", 1000);
ROMAN_SERIAL_NUMBER_MAP.put("IV", 4);
ROMAN_SERIAL_NUMBER_MAP.put("iv", 4);
ROMAN_SERIAL_NUMBER_MAP.put("IX", 9);
ROMAN_SERIAL_NUMBER_MAP.put("ix", 9);
ROMAN_SERIAL_NUMBER_MAP.put("XL", 40);
ROMAN_SERIAL_NUMBER_MAP.put("xl", 40);
ROMAN_SERIAL_NUMBER_MAP.put("XC", 90);
ROMAN_SERIAL_NUMBER_MAP.put("xc", 90);
ROMAN_SERIAL_NUMBER_MAP.put("CD", 400);
ROMAN_SERIAL_NUMBER_MAP.put("cd", 400);
ROMAN_SERIAL_NUMBER_MAP.put("CM", 900);
ROMAN_SERIAL_NUMBER_MAP.put("cm", 900);
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(1, "i");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(5, "v");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(10, "x");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(50, "l");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(100, "c");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(500, "d");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(1000, "m");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(4, "iv");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(9, "ix");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(40, "xl");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(90, "xc");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(400, "cd");
NUM_TO_LOWER_ROMAN_SERIAL_NUMBER.put(900, "cm");


NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(1, "I");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(5, "V");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(10, "X");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(50, "L");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(100, "C");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(500, "D");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(1000, "M");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(4, "IV");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(9, "IX");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(40, "XL");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(90, "XC");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(400, "CD");
NUM_TO_UPPER_ROMAN_SERIAL_NUMBER.put(900, "CM");

DIGITS[0] = 1000;
DIGITS[1] = 900;
DIGITS[2] = 500;
DIGITS[3] = 400;
DIGITS[4] = 100;
DIGITS[5] = 90;
DIGITS[6] = 50;
DIGITS[7] = 40;
DIGITS[8] = 10;
DIGITS[9] = 9;
DIGITS[10] = 5;
DIGITS[11] = 4;
DIGITS[12] = 1;
}

/**
* 数字转小写罗马字符串
*
* @param num 字符串
* @return 数字
*/
public static String toLowerRomanSerialNumber(int num) {
return toRomanSerialNumber(num, true);
}
/**
* 数字转大写罗马字符串
*
* @param num 字符串
* @return 数字
*/
public static String toUpperRomanSerialNumber(int num) {
return toRomanSerialNumber(num, false);
}
/**
* 数字转罗马字符串
*
* @param num 字符串
* @return 数字
*/
public static String toRomanSerialNumber(int num,boolean lowerChar) {
Map<Integer, String> chars = lowerChar ? NUM_TO_LOWER_ROMAN_SERIAL_NUMBER : NUM_TO_UPPER_ROMAN_SERIAL_NUMBER;
String res = "";
while (num > 0) {
for (int digit : DIGITS) {
if (num >= digit) {
res += chars.get(digit);
num -= digit;
break;
}
}
}
return res;
}

/**
* 罗马字符串转 数字
*
* @param roman 字符串
* @return 数字
*/
public static int getLowerRomanSerialNumber(String roman) {
int res = 0;
int idx = 0;
while (idx < roman.length()) {
if (idx + 1 < roman.length() && ROMAN_SERIAL_NUMBER_MAP.containsKey(roman.substring(idx, idx + 2))) {
res += ROMAN_SERIAL_NUMBER_MAP.get(roman.substring(idx, idx + 2));
idx += 2;
} else {
res += ROMAN_SERIAL_NUMBER_MAP.get(roman.substring(idx, idx + 1));
idx++;
}

}
return res;
}
}

本文地址: https://github.com/maxzhao-it/blog/post/b87a95cb/