CFLib.org – Common Function Library Project

romanToDecimal(input)

Last updated February 4, 2010
Download UDF

author

Raymond Camden Raymond Camden

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Converts Roman numerals to decimal. Assumes a valid ROman numeral.

Return Values:
Returns a number.

Example:

<cfset inputs = "XX,XI,IV,VIII,MC,DL,XL">
<cfloop index="input" list="#inputs#">
    <cfoutput>
    #input#=#romantodec(input)#<br/>
    </cfoutput>
</cfloop>

Parameters:

Name Description Required
input Roman number input. Yes

Full UDF Source:

<cfscript>
/**
* Converts Roman numerals to decimal.
* v2 fix by for non standard things like IIX, fix done by Gary Funk
*
* @param input      Roman number input. (Required)
* @return Returns a number.
* @author Raymond Camden (ray@camdenfamily.com)
* @version 2, February 4, 2010
*/

function romantodec(input) {
    var romans = {};
    var result = 0;
    var pos = 1;
    var char = "";
    var thisSum = "";
    var nextchar = "";
    var subSum = 0;
        
    romans["I"] = 1;
    romans["V"] = 5;
    romans["X"] = 10;
    romans["L"] = 50;
    romans["C"] = 100;
    romans["D"] = 500;
    romans["M"] = 1000;

    while(pos lte len(input)) {
        char = mid(input, pos, 1);
        subSum += romans[char];
        if(pos != len(input)) {
            nextchar = mid(input, pos + 1, 1);
            if(romans[char] == romans[nextchar]) {
                pos++;
            } else if(romans[char]
< romans[nextchar]) {
                result = result + romans[nextchar] - subSum;
                subSum = 0;
                pos += 2;
            } else {
                result = result + subSum;
                subSum = 0;
                pos++;
            }
        } else {
            result = result + subSum;
            pos++;
        }
    }
    return result;
}
</cfscript>

Search CFLib.org


Latest Additions

Ryan Thompson-Jewell Ryan Thompson-Jewell added
ListSplit
13 hour(s) ago

Nathan Dintenfass Nathan Dintenfass added
RowsToColumns
13 hour(s) ago

Barney Boisvert Barney Boisvert added
indentXml
23 hour(s) ago

Barney Boisvert Barney Boisvert added
REReplaceCallbac...
23 hour(s) ago

Top Rated

Rob Brooks-Bilson                                 FolderSize
Rated 5.0, 7 time(s)

Nick Giovanni                                     UniqueValueList
Rated 5.0, 5 time(s)

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Stephen Withington RandomizeString
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson