I'm trying to calculate ERA for my baseball game, which is:
(number of earned runs allowed / innings pitched) * 9
The issue comes with innings pitched. IP is calculated is thirds of an inning and i can't get a decimal to do this. Is there a way to make a decimal a fraction?
local n = 7.2; local function convert (number) local length = tostring(number):len(); local x = number; local y = 1; local a = "1"; for i = 1, length do a = a .. "0"; end a = tonumber(a); x = x * a; y = y * a; for i = x, 1, -1 do local c = x / i; local d = y / i; if (c == math.floor(c) and d == math.floor(d)) then return c, d; end end end local function convertToWhole (x, y) local total = 0; local x = x; for i = x, 1, -1 do if x >= y then x = x - y; total = total + 1; end; end local a = total; local b = x; local c = y; return a, b, c; end; local a, b, c = convertToWhole(convert(n)); print(string.format("%d %d / %d", a, b, c));