# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2009-11-23 22:16:25

Charny
Member
Registered: 2009-11-23
Posts: 1

### Scripts of fractions

decimal_to_fraction()

Expand//argument0 = the number without decimal
//argument1 = the decimal
//argument2 = reduced or not reduced (true or false)

//E.g : decimal_to_fraction(11,12) -> 11,12

//it will return a fraction like this : 11/5

var xx yy decimal number sur fraction;

decimal = string(argument1);
number = string_length(decimal);
fraction = power(10,number);
sur = argument1+(argument0*fraction);
xx = sur;
yy = fraction;

if argument2 = true
{
while (xx mod 2)=0 && (yy mod 2)=0
{
xx = xx/2;
yy = yy/2;
}
while (xx mod 3)=0 && (yy mod 3)=0
{
xx = xx/3;
yy = yy/3;
}
while (xx mod 4)=0 && (yy mod 4)=0
{
xx = xx/4;
yy = yy/4;
}
while (xx mod 5)=0 && (yy mod 5)=0
{
xx = xx/5;
yy = yy/5;
}
while (xx mod 6)=0 && (yy mod 6)=0
{
xx = xx/6;
yy = yy/6;
}
while (xx mod 7)=0 && (yy mod 7)=0
{
xx = xx/7;
yy = yy/7;
}
while (xx mod 8)=0 && (yy mod 8)=0
{
xx = xx/8;
yy = yy/8;
}
while (xx mod 9)=0 && (yy mod 9)=0
{
xx = xx/9;
yy = yy/9;
}
}
return string(xx)+"/"+string(yy)

decimal_to_fraction_whole()

Expand//argument0 = the number without decimal
//argument1 = the decimal
//argument2 = reduced or not reduced (true or false)

//E.g : decimal_to_fraction_whole(11,12) -> 11,12

//it will return a fraction like this : 2 1/5

var xx yy decimal number sur fraction;

decimal = string(argument1);
number = string_length(decimal);
fraction = power(10,number);
xx = argument1;
yy = fraction;

if argument2 = true
{
while (xx mod 2)=0 && (yy mod 2)=0
{
xx = xx/2;
yy = yy/2;
}
while (xx mod 3)=0 && (yy mod 3)=0
{
xx = xx/3;
yy = yy/3;
}
while (xx mod 4)=0 && (yy mod 4)=0
{
xx = xx/4;
yy = yy/4;
}
while (xx mod 5)=0 && (yy mod 5)=0
{
xx = xx/5;
yy = yy/5;
}
while (xx mod 6)=0 && (yy mod 6)=0
{
xx = xx/6;
yy = yy/6;
}
while (xx mod 7)=0 && (yy mod 7)=0
{
xx = xx/7;
yy = yy/7;
}
while (xx mod 8)=0 && (yy mod 8)=0
{
xx = xx/8;
yy = yy/8;
}
while (xx mod 9)=0 && (yy mod 9)=0
{
xx = xx/9;
yy = yy/9;
}
}
return string(argument0)+"  "+string(xx)+"/"+string(yy)

I have more scripts, but I find them useless... You can check them here :

http://gmc.yoyogames.com/index.php?showtopic=452897

Offline

## #2 2009-11-24 16:21:18

RaiSoleil
Member
Registered: 2009-08-02
Posts: 16

### Re: Scripts of fractions

How is 11.12 == 2 1/5? It's 278/25.

Which brings up something else. Just loop through 2 and 5, and you'll get the same answer. The other numbers are pointless.

Or, if you really want to go crazy, try something like this: http://homepage.smc.edu/kennedy_john/DEC2FRAC.PDF

Offline