How would I turn a decimal into a fraction?

I’ve searched multiple topics for a solution and I can’t find one out at all, all I want to do is turn a decimal into a fraction via math in roblox.

For example, .5 into 1/2; I’ve got no clue how to start out with this so I’m just asking here in hopes for some help.

2 Likes

do you want the fraction to be a string?
cuz 1/2 would output 0.5 if it isn’t one

1 Like

if you want the fraction to be a string I could help out

1 Like

Yeah, that’s what I’m trying to go for. (Sorry for late reply was trying out something, it didn’t work.)

ill make a function for you :smiley:

I have made an example function:

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.

1 Like

actually instead of making my own can I use yours and show how to simplify?

it would be a lot faster for me

Yeah, you can use the script. :smiley:

Google will usually have answers to questions such as these.

Here’s a good resource: Algorithm-Implementations/dec2frac.lua at master · kennyledet/Algorithm-Implementations · GitHub (2nd google search result by the way)

Example output:

local function print_fraction(...)
	print(("%d/%d"):format(fraction(...)))
end

print_fraction(0.5) -- > 1/2
print_fraction(0.25) -- > 1/4
print_fraction(1/3) -- > 1/3
print_fraction(1/25) -- > 1/25
print_fraction(1/123) -- > 1/123
print_fraction(0.6666666666666666666666) -- > 2/3
print_fraction(65872/829753) -- > 5/63

print(math.abs((65872/829753) - 5/63)) -- error of about 0.0000224, but still pretty close

As you can see, it estimates the fraction, but I doubt you’re gonna need to convert big fractions like that anyway

4 Likes

Alright, I think I got it working:

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
2 Likes

How do I mark a solution for two things? Cause both of your responses seem to literally hit the target on what I was trying to achieve.

I found a problem with your solution tho
if the decimal place gets past 5 digits long it creates a number that isn’t correct

0.1 – works
0.1111 – works
0.11111 – outputs a number that isn’t right and is too big

also the one I made does what yours does but with less code

1 Like

sometimes it can get past 5 digits such as 0.55555 works

1 Like

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.

1 Like
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

this is your code but less code into it

2 Likes

I do recommend this since the function is way smaller

but the question is, how do we get every decimal to work if they are bigger decimals?

1 Like

Yeah, this was my first time doing something like this so I just threw something together.

1 Like

I feel like im talking way to much on this post sorry

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
1 Like