GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Script Submission » Decimal to Fraction Script » 2009-06-22 20:05:15

XShortGuy
Replies: 1

I have all ready submitted this script to the site, but I figure that I might as well post it here as well:
(Questions, Comments, and Improvements are welcome.)

/*
**  Usage:
**      convert_fraction(real number)
**
**  Argument:
**      Any real number
**
**  Returns
**      A string
**
*/
{
    var number, pow, mag, gcf, numer, denom;
    number = argument0;
    pow = string_length(string_digits(frac(number)))
    mag = power(10, pow)
    gcf = gcd(mag, number * mag)
    denom = mag / gcf;
    numer = number / gcf;
    return string(numer) + "/" + string(denom);
}

This relies on a gcd script, such as this one found on the site:

/*
**  Usage:
**      gcd(a,b)
**
**  Arguments:
**      a,b     non-negative integers
**
**  Returns:
**      the greatest common divisor of the given integers
**
**  GMLscripts.com
*/
{
    var a,b,r;
    a = max(argument0,argument1);
    b = min(argument0,argument1);
    while (b != 0) {
        r = a mod b;
        a = b;
        b = r;
    }
    return a;
}

Board footer

Powered by FluxBB