CFLib.org – Common Function Library Project

makeFraction(decimalNumber)

Last updated April 7, 2010

author

Melissa Newlin

Version: 0 | Requires: CF5 | Library: MathLib

Description:
Turns a decimal number into a reduced fraction.

Return Values:
Returns a string.

Example:

<cfoutput>
#makeFraction(2.125)#
</cfoutput>

Parameters:

Name Description Required
decimalNumber number to turn into reduced fraction Yes

Full UDF Source:

<!---
 Turns a decimal number into a reduced fraction.
 
 @param decimalNumber 	 number to turn into reduced fraction (Required)
 @return Returns a string. 
 @author Melissa Newlin (cyborgirlx@yahoo.com) 
 @version 0, April 7, 2010 
--->
<cffunction name="makeFraction" output="no" returntype="string" hint="turns a decimal number into a fraction">
	<cfargument name="decimalNumber" type="numeric" required="yes">
	<cfset var x=0>
	<cfset var numerator = ListLast(ARGUMENTS.decimalNumber,".")>
	<cfset var numeratorLength = Len(numerator)>
	<cfset var numFactors = "">
	<cfset var denomFactors = "">
	<cfset var denominator = 1>
	<cfset var GCD = 0>


	<cfloop from="1" to="#numeratorlength#" index="x">
		<cfset denominator = Evaluate(denominator & "0")>
	</cfloop>

	<!--- find factors of numerator --->
	<cfset x = 1>

	<cfloop condition="x NEQ numerator + 1">
		<cfif numerator MOD x EQ 0>
			<cfset numFactors = ListAppend(numFactors, x)>
		</cfif>
		<cfset x = x + 1>
	</cfloop>

	<!--- find factors of denominator --->
	<cfset x = 1>

	<cfloop condition="x NEQ denominator + 1">
		<cfif denominator MOD x EQ 0>
			<cfset denomFactors = ListAppend(denomFactors, x)>
		</cfif>
		<cfset x = x + 1>
	</cfloop>


	<!--- find greatest common divisor --->
	<cfset x = ListLen(numFactors)>

	<cfloop condition="GCD EQ 0">
		<cfif denomFactors CONTAINS ListGetAt(numFactors,x)>
			<cfset GCD = ListGetAt(numFactors,x)>
		<cfelse>
			<cfset x = x - 1>
		</cfif>
	</cfloop>

	<!--- set the "fractionalized" number --->
	<cfset numerator = numerator / GCD>
	<cfset denominator = denominator / GCD>

	<cfreturn "#ListFirst(ARGUMENTS.decimalNumber,".")# #numerator#/#denominator#">

</cffunction>
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