# 羅馬數字到阿拉伯數字在線轉換工具

## 如何進行羅馬數字到阿拉伯數字轉換?

1. 步驟1:按順序記住拉丁字母代表的數值`('I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000)` .

2. 步驟2:從左到右,如果拉丁字母出現在比它大的拉丁字母之前,減去這個拉丁字母代表的數值.

例2.1: IV = V − I = 5 − 1 = 4

例2.2: XL = X − L = 50 − 10 = 40

3. 步驟3:其他情況,增加這個拉丁字母代表的數值.

例3.1: XXXV = X + X + X + V = 10 + 10 + 10 + 5 = 35

例3.2: CXXIII = C + X + X + I + I + I = 100 + 10 + 10 + 1 + 1 + 1 = 123

CXLVI = C + XL + V + I = 100 + (500-10) + 5 + 1 = 146

CDXCIV = CD + XC + IV = (500-100) + (100-10) + (5-1) = 494

## 羅馬數字到阿拉伯數字轉換錶:

I 1 XXI 21
II 2 XXII 22
III 3 XXIII 23
IV 4 XXIV 24
V 5 XXV 25
VI 6 XXVI 26
VII 7 XXVII 27
VIII 8 XXVIII 28
IX 9 XXIX 29
X 10 XXX 30
XI 11 XXXI 31
XII 12 XXXII 32
XIII 13 XXXIII 33
XIV 14 XXXIV 34
XV 15 XXXV 35
XVI 16 XXXVI 36
XVII 17 XXXVII 37
XVIII 18 XXXVIII 38
XIX 19 XXXIX 39
XX 20 XL 40

## 用Python進行羅馬數字到阿拉伯數字轉換:

```def transform_roman_numeral_to_number(roman_numeral):
roman_char_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
res = 0
for i in range(0, len(roman_numeral)):
if i == 0 or roman_char_dict[roman_numeral[i]] <= roman_char_dict[roman_numeral[i - 1]]:
res += roman_char_dict[roman_numeral[i]]
else:
res += roman_char_dict[roman_numeral[i]] - 2 * roman_char_dict[roman_numeral[i - 1]]
return res

roman_numeral_input = 'MMXVIII'
number_output = transform_roman_numeral_to_number(roman_numeral_input)
print('Roman numeral {0} equal to:{1}'.format(roman_numeral_input, number_output))

-------------------
Roman numeral MMXVIII equal to:2018```

## 用Java進行羅馬數字到阿拉伯數字轉換:

```import java.util.Map;
import java.util.HashMap;

public class NumberConvertManager {
public static int transform_roman_numeral_to_number(String roman_numeral) {
Map<Character, Integer> roman_char_dict = new HashMap<Character, Integer>();
roman_char_dict.put('I', 1);
roman_char_dict.put('V', 5);
roman_char_dict.put('X', 10);
roman_char_dict.put('L', 50);
roman_char_dict.put('C', 100);
roman_char_dict.put('D', 500);
roman_char_dict.put('M', 1000);
int res = 0;
for (int i = 0; i < roman_numeral.length(); i += 1) {
if (i == 0 || roman_char_dict.get(roman_numeral.charAt(i)) <= roman_char_dict.get(roman_numeral.charAt(i - 1)))
res += roman_char_dict.get(roman_numeral.charAt(i));
else
res += roman_char_dict.get(roman_numeral.charAt(i)) - 2 * roman_char_dict.get(roman_numeral.charAt(i - 1));
}
return res;
}

public static void main(String[] args) {
String roman_numeral_input = "MMXVIII";
int number_output = transform_roman_numeral_to_number(roman_numeral_input);
System.out.println("Roman numeral " + roman_numeral_input + " equal to:" + number_output);
}
}

-------------------
Roman numeral MMXVIII equal to:2018
```