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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson