function ToFraction(Number)
local Number = tostring(Number)
local Table = string.split(Number, ".")
local NewNum = Table[1]..Table[2]
local len = string.len(Table[2])
return NewNum / len.."/"..10^len
end
print(ToFraction(0.5))
I don’t know how to make it simplify right now because this is my first time doing this.
function GCF(Num)
local Number = tonumber(Num)
local T = 0
repeat
T += 1
wait()
until (T / Number) == 1
return T
end
function ToFraction(Number)
local Number = tostring(Number)
local Table = string.split(Number, ".")
local NewNum = Table[1]..Table[2]
local len = string.len(Table[2])
local Factor = GCF(NewNum / len)
local FinalProduct = (NewNum / len) / Factor.."/"..10^len / Factor
return FinalProduct
end
print(ToFraction(0.5)) -- Put your number here
Have you tried your code with numbers other than 0.5? For example 0.25 seems to just never exit the repeat loop. I’m also unsure of why you have a wait() in your code, if the script crashes without a yield then you’re probably not doing something right.
function DecimalToFraction(Number)
local Number = tostring(Number)
local Table = string.split(Number, ".")
local NewNum = Table[1]..Table[2]
local len = string.len(Table[2])
local Numerator = NewNum / len
local Denominator = 10^len
local function GCF(a,b)
if b ~= 0 then
return GCF(b, a % b)
else
return math.abs(a)
end
end
local GCF = GCF(Numerator, Denominator)
return Numerator / GCF.."/"..Denominator / GCF
end
print(DecimalToFraction(0.1)) -- 1/10
print(DecimalToFraction(0.22)) -- 11/100
print(DecimalToFraction(0.333)) -- 111/1000
print(DecimalToFraction(0.4444)) -- 1111/10000
print(DecimalToFraction(0.55555)) -- 11111/100000
I’m going to mark this as the solution as I just needed a way to format decimals into fractions while being in a small form factor, @D0RYU@NeonTaco135@heII_ish Thank you for the help!
side note, probably should’ve looked more into the api before asking as well lol