wrote this over 3 years ago...never actually put it into production
use, so there are no guarantees :)

but if it doesn't work, at least it might give you a foundation to improve upon.

/** ************
*
* Converts a decimal to a fractional number
* version 1.0
*
* @param number         Decimal value to be converted to a fractional value
* @param mixedNum       YES | 1         if fraction greater than 1, returns 
mixed numerals
*                                       NO  | 0         if fraction greater 
than 1, returns a fraction
* @return fraction      Returns a fractional value as a string
*
* @author Charlie Griefer ([EMAIL PROTECTED])
* @version 1.0, March 24, 2003
*
* Algorithm obtained from http://zend.com/codex.php?id=175&single=1
* Original Author of PHP Function: J. A. Greant ([EMAIL PROTECTED])
*
* Known Issues:
* ------------
*
* 25 Mar 2003
* The value passed to the function can have no more than 9 decimal places,
* otherwise CF throws an error 'Cannot convert 10000000000 to integer.'
*
************ **/

function toFraction(number, mixedNum) {
        
        // variable declarations
        var posNeg              = 1;
        var wholeNum    = "";
        var numerator   = "";
        var denominator = "";
        var fraction    = "";

        // make sure the decimal is not in the first or last position
        if ((left(trim(number), 1)) IS '.') number = "0" & number;
        if ((right(trim(number), 1)) IS '.') number = number & "0";

        // return if invalid number (return 0) or an integer (return integer/1)
        if (NOT isNumeric(number)) return 0;
        if ((NOT find('.', number)) OR (NOT listLast(number, '.'))) {
                fraction = int(number) & "/1";
                return fraction;
        }
        
        if (number LT 0) {
                posNeg = -1;
        }

        wholeNum        = abs(val(listFirst(number, ".")));
        numerator       = abs(val(listLast(number, ".")));
        denominator     = "1";
        
        for (i=1; i LTE len(numerator); i=i+1) {
                denominator = denominator & "0";
        }

        gcd = getGCD(numerator, denominator, number);
        
        numerator       = numerator/gcd;
        denominator     = denominator/gcd;
        
        if (mixedNum AND wholeNum) {
                fraction = posNeg * wholeNum & " <span
style=""font-size:8px;""><sup>" & abs(numerator) & "</sup>/<sub>" &
denominator & "</sub></span>";
        } else {
                numerator = wholeNum * denominator + numerator;
                fraction = posNeg * numerator & "/" & denominator;
        }
        return fraction;
}

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.

-- 
Charlie Griefer

================================================
"...All the world shall be your enemy, Prince with a Thousand Enemies,
and whenever they catch you, they will kill you. But first they must catch
you, digger, listener, runner, prince with a swift warning.
Be cunning and full of tricks and your people shall never be destroyed."

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
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:249736
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to