This page shows samples of how my getFraction function for ColdFusion works.
You can Download the function at the bottom of the page.
This function will take input from a decimal and return a fraction string, i.e. 3.25 will return 3 1/4.
The function takes up to 5 arguments:
- Att_number (required - type="numeric"): This is the decimal number to be converted
- Att_simplify (optional type="boolean" default="true"): (True or False) If this parameter is true, the fraction will be simplfied. If it is set to false, a fraction that works out to 32/64 would be displayed as such.
- Att_round (optional type="boolean" default="false"): (True or False) If this parameter is true, the number will be rounded up or down to the nearest number that when multiplied by the denominator becomes a whole number. With a denominator set to "10", "0.524" would become "0.5", and rendered as "5/10" or "1/2"
- Att_blnHyphen (optional type="boolean" default="false"): (True or False) If this is true then a hyphen will separate the number from the fraction, i.e. 3.25 will return 3-1/4
- Att_denominatorLst (optional type="string" default="64,10000"): A list of denominators to try for the "bottom" number of a fraction. By default the denominator list is set to "64,10000", which would render fractions such as "1/64", "2/64", "3/20", 1/5", etc. If Simplify is set to False then only the first value in this list will be used.
|
| FUNCTION |
RETURNS |
| getFraction(Att_number,Att_simplify,Att_round,Att_blnHyphen,Att_denominatorLst)
| Returns Number |
| getFraction(3.33,1,0,1) |
3-33/100 |
| getFraction(3.33,0,1,1) |
3-21/64 |
| getFraction(3.33,1,1,1) |
3-33/100 |
| getFraction(3.33,0,0,1) |
3.33 |
| getFraction(3.33,1,0,0) |
3 33/100 |
| getFraction(3.33,0,1,0) |
3 21/64 |
| getFraction(3.33,1,1,0) |
3 33/100 |
| getFraction(3.33,0,0,0) |
3.33 |
| getFraction(3.33,1,0,1,"10000") |
3-33/100 |
| getFraction(3.33,0,1,1,"10000") |
3-3300/10000 |
| getFraction(3.33,1,1,1,"10000") |
3-33/100 |
| getFraction(3.33,0,0,1,"10000") |
3-3300/10000 |
| getFraction(3.33,1,0,0,"10000") |
3 33/100 |
| getFraction(3.33,0,1,0,"10000") |
3 3300/10000 |
| getFraction(3.33,1,1,0,"10000") |
3 33/100 |
| getFraction(3.33,0,0,0,"10000") |
3 3300/10000 |
| |
| getFraction(4.95,1,0) |
4 19/20 |
| getFraction(4.95,0,1) |
4 61/64 |
| getFraction(4.95,1,1) |
4 19/20 |
| getFraction(4.95,0,0) |
4.95 |
| |
| getFraction(3.25,1,0) |
3 1/4 |
| getFraction(3.25,0,1) |
3 16/64 |
| getFraction(3.25,1,1) |
3 1/4 |
| getFraction(3.25,0,0) |
3 16/64 |
| |
| getFraction(16.0625,1,0) |
16 1/16 |
| getFraction(16.0625,0,1) |
16 4/64 |
| getFraction(16.0625,1,1) |
16 1/16 |
| getFraction(16.0625,0,0) |
16 4/64 |
| |
| getFraction(-5.875,1,0) |
-5 7/8 |
| getFraction(-5.875,0,1) |
-5 56/64 |
| getFraction(-5.875,1,1) |
-5 7/8 |
| getFraction(-5.875,0,0) |
-5 56/64 |
| |
| getFraction(5.2,1,0) |
5 1/5 |
| getFraction(5.2,0,1) |
5 13/64 |
| getFraction(5.2,1,1) |
5 1/5 |
| getFraction(5.2,0,0) |
5.2 |
| |
| getFraction(5.8,1,0) |
5 4/5 |
| getFraction(5.8,0,1) |
5 51/64 |
| getFraction(5.8,1,1) |
5 4/5 |
| getFraction(5.8,0,0) |
5.8 |
| |
Download getFraction() function:
<!--- =============== GETFRACTION() - CONVERTS A DECIMAL TO A FRACTION. =============== --->
<CFFUNCTION name="getFraction" returnType="string" output="yes" hint="This function will take input from a decimal and return a fraction string, i.e. 3.25 will return 3 1/4.">
<!--- required argument --->
<CFARGUMENT name="Att_number" required="True" type="numeric"> <!--- This is the decimal number to be converted --->
<!--- optional arguments --->
<CFARGUMENT name="Att_simplify" required="false" type="boolean" default="true"><!--- (True or False) If this parameter is true, the fraction will be simplfied. If it is set to false, a fraction that works out to 32/64 would be displayed as such. --->
<CFARGUMENT name="Att_round" required="false" type="boolean" default="false"><!--- (True or False) If this parameter is true, the number will be rounded up or down to the nearest number that when multiplied by the denominator becomes a whole number. With a denominator set to "10", "0.524" would become "0.5", and rendered as "5/10" or "1/2" --->
<CFARGUMENT name="Att_blnHyphen" required="false" type="boolean" default="false"><!--- (True or False) If this is true then a hyphen will separate the number from the fraction, i.e. 3.25 will return 3-1/4 --->
<CFARGUMENT name="Att_denominatorLst" required="false" type="string" default="64,10000"><!--- A list of denominators to try for the "bottom" number of a fraction. By default the denominator list is set to "64,10000", which would render fractions such as "1/64", "2/64", "3/20", 1/5", etc. If Simplify is set to False then only the first value in this list will be used.--->
<!---
###################################################
## Author: George Jaros ##
## Script Name: ColdFusion getFraction Function ##
## Copyright 2007 George Jaros & Web 2 Market ##
## www.georgejaros.com www.web2market.com ##
## This code may be replicated as long as ##
## this header statement is included. ##
###################################################
--->
<CFIF Att_blnHyphen>
<CFSET strSep = "-">
<CFELSE>
<CFSET strSep = " ">
</CFIF>
<CFIF #IsNumeric(Evaluate(Att_number))# EQ 'true'>
<CFSET LastAtt_denominator = 0>
<CFIF #Att_simplify# EQ 'true' AND #Evaluate(Att_number)# NEQ 0>
<CFLOOP list="#Att_denominatorLst#" index="Att_denominator">
<CFSET Att_denominator = Val(Att_denominator)>
<CFSET blnDenomFound = False>
<CFLOOP CONDITION="#Int(Att_number*Int(Att_denominator/11))# EQ (Att_number*Att_denominator/11)">
<CFSET Att_denominator=#Evaluate(Att_denominator/11)#>
<CFSET blnDenomFound = True>
</CFLOOP>
<CFLOOP CONDITION="#Int(Att_number*Int(Att_denominator/7))# EQ (Att_number*Att_denominator/7)">
<CFSET Att_denominator=#Evaluate(Att_denominator/7)#>
<CFSET blnDenomFound = True>
</CFLOOP>
<CFLOOP CONDITION="#Int(Att_number*Int(Att_denominator/5))# EQ (Att_number*Att_denominator/5)">
<CFSET Att_denominator=#Evaluate(Att_denominator/5)#>
<CFSET blnDenomFound = True>
</CFLOOP>
<CFLOOP CONDITION="#Int(Att_number*Int(Att_denominator/3))# EQ (Att_number*Att_denominator/3)">
<CFSET Att_denominator=#Evaluate(Att_denominator/3)#>
<CFSET blnDenomFound = True>
</CFLOOP>
<CFLOOP CONDITION="#Int(Att_number*Int(Att_denominator/2))# EQ (Att_number*Att_denominator/2)">
<CFSET Att_denominator=#Evaluate(Att_denominator/2)#>
<CFSET blnDenomFound = True>
</CFLOOP>
<CFIF blnDenomFound>
<CFIF LastAtt_denominator GT 0>
<CFSET Att_denominator = Min(Att_denominator,LastAtt_denominator)>
</CFIF>
<CFSET LastAtt_denominator = Att_denominator>
</CFIF>
</CFLOOP>
</CFIF>
<CFIF LastAtt_denominator GT 0>
<CFSET Att_denominator = LastAtt_denominator>
<CFELSE>
<CFSET Att_denominator = ListFirst(Att_denominatorLst)>
</CFIF>
<CFIF #Int(Att_number)# NEQ #Att_number#>
<CFIF (#Int(Att_number * Att_denominator)# IS (Att_number * Att_denominator)) OR (Att_round IS 'true')>
<CFIF #Att_number# LT 1 AND #Att_number# GT -1>
<CFIF #Att_round# EQ 'true'>
<CFSET #Att_number#=(#Round(Att_number*Att_denominator)# / #Att_denominator#)>
</CFIF>
<CFRETURN "#Evaluate(Att_number*Att_denominator)#/#Att_denominator#">
<CFELSE><!--- make complex fraction --->
<CFIF #Att_number# GT 1>
<CFRETURN "#Int(Att_number)##strSep##Evaluate(Round((Att_number - Int(Att_number))*Att_denominator))#/#Att_denominator#">
<CFELSE><!--- make negative complex fraction --->
<CFRETURN "#Ceiling(Att_number)##strSep##Evaluate((Att_number-(2*Att_number)-Int(Att_number-(2*Att_number)))*Att_denominator)#/#Att_denominator#">
</CFIF>
</CFIF>
<CFELSE>
<CFRETURN "#Evaluate(Att_number*1)#">
</CFIF>
<CFELSE>
<CFRETURN "#Evaluate(Att_number*1)#">
</CFIF>
<CFELSE>
<CFRETURN "#Att_number#">
</CFIF>
</CFFUNCTION>
<!--- =============== END GETFRACTION() =============== --->
|