📜 ⬆️ ⬇️

From Basra to Rome. Convert Arabic numbers to Roman

Good time of day, Habrayuzer.

Developing mainly for the J2ME platform, I always tried to bring some highlights to my projects. So, one day, I needed to highlight the menu items.

I didn’t really want to use a standard solution, using a dash or enumerating them, but I had no other options then. Thinking about how to arrange these items, I accidentally stumbled upon my old school history notebook, in which the items were numbered using Roman numerals. Bingo!
')
It is worth admitting that at that time my knowledge of Roman numerals was enough only for counting up to the second and third dozen. That in general, then for the numbering of the points should have been enough. But like any programmer, I wanted to have a ready-made solution that could translate any integer decimal Arabic numbers into equivalent Roman ones.

An article describing the Roman SS was found on Wikipedia, and soon I sketched an algorithm for converting Arabic to Roman numbers.

Key numbers:

I - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 1000

Zero is absent altogether, so we will use an empty value.

So, the algorithm itself is quite simple:

1. Select (if any) the number of whole thousands. The resulting value allows us to generate a string with n number "M" (read, n * 1000).
Example: 2012 after the first paragraph will give "MM"

2. We get the remainder after dividing by 1000, to further highlight the following values.

3. Select (if possible), whole 500. At the same time, we take into account that if the value obtained is 4 (5 + 4 = 9), then it should be recorded as a value of 1000-100, which in the Roman SS is equivalent to “CM”.
Example: 1887 after this item will give us "MD".
1945, respectively, "MCM".

4. We get the remainder of the division by 500.

5. Divide by 100 to select the whole hundreds and add to the previous result. We take into account that if we received 4, which is equivalent to 400, then we write it down as 500-100, that is, “CD”.
Example: 1709 will give after this step "MDCCC".

6. Get the remainder of the division by 100.

7. We select from it the whole half hundreds. If the value is 4 (that is, 90), then we write it as 100-10, which is equal to "XC". Otherwise, add to the line "L"
Example: 1986 after all will give us the "MCML".

8. Select the remainder from 50.

9. Select the integer number of tens and add the character “X” to the string n times. In this case, we consider that 40 is written as 50-10, that is, "XL".
Example: 1986 after all will give us "MCMLXXX".

10. We get the remainder of division by 10. This step differs from the others in that you can immediately equate the remainder to its equivalent. 1 = I, 7 = VII and so on.

After iterating through a number with this algorithm, we get something like this:
2012 == MMXII
Below is the Java source code that implements the algorithm.
public class RoManiac { /*  : I - 1 V - 5 X - 10 L - 50 C - 100 D - 500 M - 1000   ,     */ //        public static String convert(int in) { StringBuffer a = new StringBuffer(""); //   int m1 = in / 1000; a.append(M(m1)); //      int m2 = in % 1000; //       int d1 = m2 / 500; a.append(D(d1)); //     int d2 = m2 % 500; //     int c1 = d2 / 100; a.append(C(c1)); //    int c2 = d2 % 100; //   int l1 = c2 / 50; a.append(L(l1)); //  int l2 = c2 % 50; //   int x1 = l2 / 10; a.append(X(x1)); //  int x2 = l2 % 10; //     a.append(basic(x2)); return a.toString(); } //    //  , ,   5,  private static String M(int in) { StringBuffer a = new StringBuffer(""); int i = 0; while (i < in) { a.append("M"); i++; } return a.toString(); } //    private static String C(int in) { if (in == 4) return "CD"; // 400,  500-100 else if ((in != 0) && (in < 4)) { StringBuffer a = new StringBuffer(""); int i = 0; while (i < in) { a.append("C"); i++; } return a.toString(); } else return ""; } //   private static String X(int in) { if (in == 4) return "XL"; //  40,  50-10 else if ((in != 0) && (in < 4)) { StringBuffer a = new StringBuffer(""); int i = 0; while (i < in) { a.append("X"); i++; } return a.toString(); } else return ""; } //    private static String D(int in) { if (in = 4) return "CM"; //  900,  1000-100 else return "D"; } private static String L(int in) { if (in = 4) return "XC"; / / 90 100 - 10 return "L"; } //  1  9,    private static String basic(int in) { String[] a = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; return a[in]; } } </code> PS:          . 

Source: https://habr.com/ru/post/136646/


All Articles