Last active
April 15, 2020 21:50
-
-
Save JamoCA/dd2d4060f59fbafbcc5e to your computer and use it in GitHub Desktop.
Dynamic fraction display For ColdFusion - Displays decimal numbers in fraction format w/optional CSS styling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 5/15/2008 by Greg Nilsen http://www.gregnilsen.com/2008/05/15/dynamic-fraction-display-for-coldfusion/ | |
10/5/2015 by SunStar Media. Tweaks. Added optional 2nd arguement to add CSS styles. | |
4/15/2020 by SunStar Media. Updated to CFScript. ---> | |
<cfscript> | |
string function displayFraction(required string inputString, boolean useHTML=false) hint="Generates a fraction from a decimal." { | |
var formatThis = val(arguments.inputString); | |
var wholePart = int(formatThis); | |
var fractionPart = (numberFormat(formatThis,".9999") - int(formatThis)) * 100; | |
var fraction = ""; | |
var denominator = ""; | |
var numerator = ""; | |
var d = 0; | |
if (wholePart GT 0){ | |
fraction = "#wholePart# "; | |
} | |
if (fractionPart NEQ 0){ | |
for (d = 2; d <= 100; d++) { | |
if ((round(fractionPart * d) MOD 100) EQ 0){ | |
denominator = d; | |
numerator = round(fractionPart * d) / 100; | |
break; | |
} | |
} | |
if (fractionPart NEQ 0){ | |
if (arguments.useHTML){ | |
if (numerator LTE 9 AND denominator LTE 9){ | |
fraction = fraction & "&frac#numerator##denominator#;"; | |
} else { | |
fraction = fraction & "<span style=""font-size:75%;""><sup>#numerator#</sup>⁄<sub>#denominator#</sub></span>"; | |
} | |
} else { | |
fraction = fraction & "#numerator#/#denominator#"; | |
} | |
} | |
} | |
if (NOT LEN(fraction)){ | |
fraction = "0"; | |
} | |
return trim(fraction); | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment