ShortTostring -- old behaviour of tostring

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.

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

2 Likes

Maybe I’m wrong but couldn’t you just round the number with a lot of decimal places?

No because you might still want some decimals, so you’d have to do a multiplication, math.round and then a division

1 Like

Nah. You want some decimals to be kept right? so this unfortunately won’t work

btw adding that to why you should use my method list lol

You could just choose how many decimals you want to keep though can’t you?

it’s the old behaviour of Tostring, so you can get a string version of the expected result only. Without those unnessecary decimals.

And what if you’re for example, prompting a player to write a number with decimal, this won’t always work.

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.

Thanks for bringing this up. If the name is misleading, what should I rename it toM

sorry for the bump, but you can do this using string.format:

print(string.format("%.14g", 0.1 + 0.2)) -- 0.3
print(0.1 + 0.2) -- 0.30000000000000004 

Good idea. It’s just that you can already do this.

1 Like