Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- code compiled by Roblox-TS
- local _exports;
- local getSignificantDigits = function(n)
- local str = tostring(n);
- local a, b, num = str:find("(%-?%d+%.?%d*)");
- if b then
- local magStr = str:match("e([+-]%d+)", b + 1);
- local magnitudeStr = (magStr and ("e") .. magStr or "");
- if tonumber(str) == n then
- return str;
- else
- str = ((num)):sub(1, -2);
- local diff = tostring(n - tonumber((str) .. magnitudeStr));
- local extraDigitA, extraDigitB, extraDigitC = diff:match("%-?(%d+)%.?(%d*)e?%-?(%d*)");
- if extraDigitA then
- local extraDigits = ((extraDigitA)) .. extraDigitB;
- local posA = str:find(".", 1, true);
- local firstSigFig = (#str) - (posA or 0) + 1;
- if extraDigitC ~= "" then
- extraDigits = (("0"):rep((tonumber(extraDigitC) or 0) - firstSigFig)) .. extraDigits;
- else
- extraDigits = extraDigits:sub(firstSigFig + 1);
- end;
- do
- local i = 1;
- while tonumber((str) .. magnitudeStr) ~= n do
- str = (str) .. (extraDigits:sub(i, i));
- if i > 200 then
- return error(("Couldn't get accurate number: ") .. n);
- end;
- i = i + 1;
- end;
- end;
- return (str) .. magnitudeStr;
- end;
- end;
- end;
- return error(("Couldn't match number: ") .. n);
- end;
- _exports = getSignificantDigits;
- return _exports;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement