Far easier to convert them from fraction to float than from float to
fraction.  Not to mention storing fractions is more precise.  So i'd
investigate a way to do that if at all possible, rather than the
converse.  However, it'd be pretty simple to write a UDF to convert
common floats back into fractions.  Here's a simple one:

<cfscript>
        function floatToFraction(num, maxDenom) {
                var denom = "";
                var numer = "";
                var intPart = fix(num);
                var sigFigs = "";
                if (num EQ intPart) {
                        return num;
                }
                num = num - intPart;
                sigFigs = len(num) - 2;
                for (denom = 2; denom LTE maxDenom; denom = denom + 1) {
                        for (numer = 1; numer LT denom; numer = numer + 1) {
                                if (round(numer / denom * (10 ^ sigFigs)) / (10 
^ sigFigs) EQ num) {
                                        if (intPart NEQ 0) {
                                                return intPart & " " & numer & 
"/" & denom;
                                        } else {
                                                return numer & "/" & denom;
                                        }
                                }
                        }
                }
                return intPart + num;
        }
</cfscript>

<cfoutput>
<table border="1">
<cfloop list="1,0.5,0.25,0.3333,0.2,0.1666667,0.125,0.1" index="i">
        <tr>
                <td>#i#</td>
                <td>#floatToFraction(i, 5)#</td>
                <td>#floatToFraction(i, 10)#</td>
        </tr>
</cfloop>
</table>
</cfoutput>

cheers,
barneyb

On 8/14/06, Andy Matthews <[EMAIL PROTECTED]> wrote:
> Does anyone happen to have a UDF that would convert decimals to their
> fractional equivalents?
>
> I'm working on a cooking website and I'm storing the quantities as floats,
> but I'd like to display them as fractions for American cooks, while still
> retaining the ability to display as decimals if needed.
>

-- 
Barney Boisvert
[EMAIL PROTECTED]
360.319.6145
http://www.barneyb.com/

Got Gmail? I have 100 invites.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four 
times a year.
http://www.fusionauthority.com/quarterly

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:249734
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to