Hey there guys! Having a good day? Well I made an extremely simple resource . Remember Roblox made an update where tostring will start returning millions of decimals, just for accuracy. But some people prefer old behaviour of tostring. so I made this! With logarithms(thank God they exist) I made this very simple resource
So basically, it’s just the old behaviour of tostring.
Enjoy.
function shorttostring(numb)
local Len = math.floor(math.log10(numb)+.5)
return string.sub(tostring(numb),1,Len)
end
print(tostring(.3)) -- 0.3000002 (not sure but I'm sure it brings so much decimals)
print(shorttostring(.3)) -- 0.3
why you want to use this over other methods
Alright, here’s why you should use this over other methods.
String.format %.f
yes it does Indeed help remove the millions of decimals, but what if you want to keep the decimals without the millions of decimals, you might want to use my method because %.f removes all decimals I believe.
I don’t know any alternatives but tell me if there is
Feedback is appreciated
Btw I made this in pure Lua which isn’t really needed to be in pure Lua, so that’s why I have stuff like floor + .5
The “old” tostring behaviour is platform (compiler) dependant. You have to check the platform to be fully able to reproduce the old behaviour along with checking edge cases as you do not seem to account for IEEE 754 special values (Infinity, different kinds of NaNs, or subnormal numbers).
For Win32 systems (which Roblox was compiled by MSVC), before Roblox was compiled by MSVC 2015(?), Infinity used to print 1.#INF, quiet NaN used to print 1.#QNAN, signaling NaN used to print 1.#SNAN, and indefinite NaN used to print 1.#IND; you’ll also notice that it rounds the value to 17 significant digits regardless of the settings of the format.
ShortToString is rather misleading name. The “new” behaviour is already in the “shortest” decimal representation of IEEE 754 double precision floating point. The old behaviour just rounds the value by 14 significant digits.
Sorry but your implementation of this is severely flawed. log10(x) finds the y of 10y = x - log10(0.3) “rounded” (floored with value added by 0.5) to integer is -1 which gets the last index of the string (in which you used string.sub) - keep in mind that string library functions have index starting at 1 rather than 0 and includes the upper bound.
Alongside that, you’re still relying on the “new” tostring behaviour, evidenced by a tostring call.
The “new” tostring behaviour is similar to the one used in V8 (Chromium-based browsers, Node.js), and CPython after 3.1.