Question: In data sometimes the same product will be named with a roman numeral while other times it will be a digit.

**Example** `Samsung Galaxy SII` verses `Samsung Galaxy S2`

How can the `II` be converted to the value `2`?

Answer: 
I landed here searching for a small implementation of a Roman Numerals parser but wasn't satisfied by the provided answers in terms of size and elegance. I leave my final, recursive implementation here, to help others searching a small implementation.

---

### Convert Roman Numerals by Recursion

* The algorithm is able to handle numerals in irregular subtractive notation (f.e. `XIIX`).
* This implementation may only work with well-formed (strings matching `/[mdclxvi]*/i`) roman numerals.
* The implementation is not optimized for speed.

```cs
// returns the value for a roman literal
private static int romanValue(int index)
{
    int basefactor = ((index % 2) * 4 + 1); // either 1 or 5...
    // ...multiplied with the exponentation of 10, if the literal is `x` or higher
    return index > 1 ? (int) (basefactor * System.Math.Pow(10.0, index / 2)) : basefactor;
}

public static int FromRoman(string roman)
{
    roman = roman.ToLower();
    string literals = "mdclxvi";
    int value = 0, index = 0;
    foreach (char literal in literals)
    {
        value = romanValue(literals.Length - literals.IndexOf(literal) - 1);
        index = roman.IndexOf(literal);
        if (index > -1)
            return FromRoman(roman.Substring(index + 1)) + (index > 0 ? value - FromRoman(roman.Substring(0, index)) : value);
    }
    return 0;
}

```

> 
> **Try it** using this .Netfiddle: <https://dotnetfiddle.net/veaNk3>
> 
> 
> 

### How does it work?

This algorithm calculates the value of a Roman Numeral by taking the highest value from the Roman Numeral and adding/subtracting recursively the value of the remaining left/right parts of the literal.

```
ii X iiv # Pick the greatest value in the literal `iixiiv` (symbolized by uppercase)

```

Then recursively reevaluate and subtract the lefthand-side and add the righthand-side:

```
(iiv) + x - (ii) # Subtract the lefthand-side, add the righthand-side
(V - (ii)) + x - ((I) + i) # Pick the greatest values, again
(v - ((I) + i)) + x - ((i) + i) # Pick the greatest value of the last numeral compound

```

Finally the numerals are substituted by their integer values:

```
(5 - ((1) + 1)) + 10 - ((1) + 1)
(5 - (2)) + 10 - (2)
3 + 10 - 2
= 11

```
```
/*
this uses the string object Replace() & Split() methods
*/
int ToNumber(string roman){
    /*
    the 0 padding after the comma delimiter allows adding up the extra index produced by Split, which is not numerical
    */
    string s1=roman.Replace("CM","900,0");
    s1=s1.Replace("M","1000,0");
    s1=s1.Replace("CD","400,0");
    s1=s1.Replace("D","500,0");
    s1=s1.Replace("XC","90,0");
    s1=s1.Replace("C","100,0");
    s1=s1.Replace("XL","40,0");
    s1=s1.Replace("L","50,0");
    s1=s1.Replace("IX","9,0");
    s1=s1.Replace("X","10,0");
    s1=s1.Replace("IV","4,0");
    s1=s1.Replace("V","5,0");
    s1=s1.Replace("I","1,0");

    string[] spl=s1.Split(",");
    int rlt=0;
    for(int i=0;i<spl.Count();i++)
    {
        rlt+= Convert.ToInt32(spl[i].ToString());
    }               
    return rlt;
}

```