Roman Numerals to Arabic Integers Converter with API

The Roman Number Convertertool is also availble in Chinese: 罗马数字转换工具

The first Few Roman Numerals go like this:

  1. I
  2. II
  3. III
  4. IV
  5. V
  6. VI
  7. VII
  8. VIII
  9. IX
  10. X
  11. XI
  12. XII
  13. XIII
  14. XIV
  15. XV

You can either put Roman numerials or the Arabic Integers in the following form. The tool will convert one to another.

Roman/Arabic Numerals

Roman to Arabic Conversion Mapping Algorithms

This tool uses the following mappings:
$r = array(           
    "m" => 1000000,
    "cm" => 900000,
    "d" => 500000,
    "cd" => 400000,
    "c" => 100000,
    "xc" => 90000,  
    "l" => 50000,
    "xl" => 40000,
    "x" => 10000,
    "Mx" => 9000,
    "v" => 5000,
    "Mv" => 4000,
    "M" => 1000,
    "CM" => 900,
    "D" => 500,
    "CD" => 400,
    "C" => 100,
    "XC" => 90,
    "L" => 50,
    "XL" => 40,
    "XXX" => 30,
    "XX" => 20,
    "X" => 10,
    "IX" => 9,
    "V" => 5,
    "IV" => 4,
    "I" => 1        
);   

To Convert Roman Numerals to Arabic Integers, you can refer to this post: How to Convert Roman to Integer in C/C++?

To Convert Arabic Integers to Roman Numerals, you can refer to this algorithm: How to Convert Arabic Integers to Roman Numerals?

Javascript Function to Convert Roman Numerals to Integer

/**
 * @param {string}
 * @return {number}
 */
var romanToInt = function(s) {
    let r = 0;
    const rules = {
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000        
    };
    for (let i = 0; i + 1 < s.length; ++ i) {
        const cur = rules[s.charAt(i)];
        const next = rules[s.charAt(i + 1)];
        if (cur >= next) {
            r += cur;
        } else {
            r -= cur;
        }
    }
    return r + rules[s.charAt(s.length - 1)];
};

Javascript Function to Convert the Arabic Integers to Roman Numerals

/**
 * @param {number} num
 * @return {string}
 */
var intToRoman = function(num) {
    const rules = {
        "M" : 1000,
        "CM" : 900,
        "D" : 500,
        "CD": 400,
        "C":  100,
        "XC":  90,
        "L":   50,
        "XL":  40,
        "XXX": 30,
        "XX":  20,
        "X":   10,
        "IX":   9,
        "V":    5,
        "IV":   4,
        "I":    1          
    }
    let res = "";
    const romans = Object.keys(rules);
    for (let i = 0; i < romans.length; ++ i) {
        const val = rules[romans[i]];
        while (num >= val) {
            num -= val;
            res += romans[i];
        }
    }
    return res;
};

Roman Numerals (API)

We provide a simple API (subject to fair use policy) that convert between Roman Numerals and Arabic integer.

API example:

https://romans.justyy.workers.dev/api/romans/?cached&n=1234
  
returns: 
​{
  "error": null,
  "input": "1234",
  "result": "MCCXXXIV"
}

https://romans.justyy.workers.dev/api/romans/?cached&n=MCCXXXIV

returns: 
​{
  "error": null,
  "input": "MCCXXXIV",
  "result": "1234"
}

You can call the Converter API via the POST method, for example:

? curl -s -X POST https://romans.justyy.workers.dev/api/romans/?cached -d "n=1234"
{"error":null,"input":"1234","result":"MCCXXXIV"}

If you are using POST method, the result will not be cached via the CloudFlare CDN, as the GET method does (via ?cached URL).

Share: List of Many Other Online Tools