CFLib.org – Common Function Library Project

fractionToDecimal(fraction)

Last updated August 16, 2008

author

Trevor Orr

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Convert fractions to decimal.

Return Values:
Returns a number.

Example:

<cfoutput>
1 1/2: #fractionToDecimal("1 1/2")#<br />
3/8: #fractionToDecimal("3/8")#<br />
12 13/64: #fractionToDecimal("12 13/64")#<br />
</cfoutput>

Parameters:

Name Description Required
fraction Value to convert to decimal. Yes

Full UDF Source:

/**
 * Convert fractions to decimal.
 * 
 * @param fraction      Value to convert to decimal. (Required)
 * @return Returns a number. 
 * @author Trevor Orr (fractorr@yahoo.com) 
 * @version 1, August 16, 2008 
 */
function fractionToDecimal(fraction) {
    var thisNumber        = 0;
    var thisFraction    = 0;
    var thisOut         = "0.0";

    if (ListLen(arguments.fraction, " ") EQ 1) {
        if (Trim(arguments.fraction) contains  "/") {
            thisOut = Val(ListFirst(arguments.fraction, "/")) / Val(ListLast(arguments.fraction, "/"));
        } else {
            thisOut = Val(Trim(arguments.fraction));
        }
    } else {
        if (Trim(ListLast(arguments.fraction, " ")) contains  "/") {
            thisOut = Val(ListFirst(arguments.fraction, " "));
            thisOut = thisOut + Val(ListFirst(ListLast(arguments.fraction, " "), "/")) / Val(ListLast(ListLast(arguments.fraction, " "), "/"));
        } else {
            thisOut = Val(Trim(arguments.fraction));
        }
    }

    return thisOut;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson