Invert GMLscripts.com

roman_to_dec

Converts a string of Roman numerals to a decimal value.

dec = roman_to_dec("XLII");     //  dec == 42
dec = roman_to_dec("MCMXCIX");  //  dec == 1999
dec = roman_to_dec("MMDXXV");   //  dec == 2525
roman_to_dec(roman)
Returns an integer converted from a Roman numeral string.
COPY/// @func   roman_to_dec(roman)
///
/// @desc   Returns an integer converted from a Roman numeral string.
///         Note: The largest value supported is 4999.
///
/// @arg    {string}    roman       roman numeral
///
/// @return {real}      positive integer
///
/// GMLscripts.com/license

function roman_to_dec(roman)
{
    var dec = 0;

    var tab, val;
    tab = "MDCLXVI";
    val[0] = 0;   val[1] = 1000;  val[2] = 500;  val[3] = 100;
    val[4] = 50;  val[5] = 10;    val[6] = 5;    val[7] = 1;

    var v1, v2;
    v2 = val[string_pos(string_char_at(roman, 1), tab)];
    for (var i=1; i<=string_length(roman); i++) {
        v1 = v2;
        v2 = val[string_pos(string_char_at(roman, i+1), tab)];
        if (v2 <= v1) dec += v1; else dec -= v1;
    }

    return dec;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw