Im trying to percent to fraction but i cant how should i do that? and is that possible?
1 Like
ex) 10% = 100 * 0.1
20% = 100 * 0.2
30% = 100 * 0.3
40% = 100 * 0.4
50% = 100 * 0.5
local function to_frac(num)
local _, frac = math.modf(num)
return frac
end
print(to_frac(5/100)) -- 0.05
print(to_frac(10/100)) -- 0.1
print(to_frac(50/100)) -- 0.5
Is that what you want?
Maybe one of these might help
Any fractional equation will result in a number value.
a = 1/4 --> 0.25
b = 1/10 --> 0.1
c = 1/3 --> 0.3333333
d = 26/5 --> 5.2
To turn a number into a fractional number, you would have to show it in string format since number format is in decimal form. You just have to supply a denominator.
local ReturnWholeNumberWithFraction = true --Set to false if you just want the fraction without whole numbers. (Example: true --> 1 1/3, false --> 4/3)
function TurnNumberIntoFraction(Percentage,Denominator)
local Num = Percentage/100 --converts it into a number
local UnfittedNumberCount = (Num*Denominator)
local FittedNumberCount = math.floor(Num*Denominator)
local Remainder = math.abs(UnfittedNumberCount - FittedNumberCount) * Denominator
if ReturnWholeNumberWithFraction == true and Remainder ~= 0 then
return tostring(FittedNumberCount).. " " .. tostring(Remainder).. "/" ..tostring(Denominator)
elseif ReturnWholeNumberWithFraction == true and Remainder == 0 then
return tostring(FittedNumberCount)
else
return tostring(Num*Denominator) .. "/" .. tostring(Denominator)
end
end
print(TurnNumberIntoFraction(520,5)) --Supplying a number percentage (520%) and a denominator (5).
1 Like