Help with turning 1e+21 into 1.00Qn

Hi i am trying to make a script for turning a number like 10.000 to 10.0K and 1.000 to 1.00K and 1.234 to 1.23K but the script i currently have does work just not for numbers over 999 trillion because the numbers start to go into the 1e+20 so how would i solve this i want it to be a function that you give in a string without anything other than the number 0-9 and then outputs what i want it to output.

this is what i have so far.

local chars = {"K", "M", "B", "T", "Qd", "Qn", "Sx", "Sp", "O", "N", "D", "UnD"}

game.ReplicatedStorage.Remote.GetShortenValue.OnServerInvoke = function(player, stat)
	local value = game.Players:FindFirstChild(player.Name).stats:FindFirstChild(stat).Value
	local val = tonumber(value)
	local length = string.len(value) - 1
	local length3 = length - (length % 3)
	local lengthDiv3 = math.floor(length / 3)
	if val < 1000 then
		return value
	else
		return tostring(math.floor(val / math.pow(10, length3 - 2)) / 100)..(chars[lengthDiv3])
	end
end

seams like no one can do this lol

take a look at this website How do I format numbers such as 10,000 as 10K? - Scripting Helpers

doesn’t really help because it wouldn’t work for very big numbers

You can format the number as a string with a certain amount of digits and then work on whatever you want.

print(string.format("%20.0f",123456789012345))

it doesn’t seam to work it just print what you put in with spaces at the front

That number was just a random thing I put in. Try typing a massive number which gets turned into scientific annotation and it will return a string of that value only containing 0-9.

again still just doesn’t work
input: 100000000000000000000000000000000000000000000000000000000000000000000000000000
output: 99999999999999998278261272554585856747747644714015897553975120217811154108416

Also a quick note about your code: it seems to be used for GUI so you could put it in a ModuleScript in the client. No need to invoke server just to get the shortened .Text value for some GUI.

That’s because your number is so high it’s hitting the floating point error. The larger the number, the more prominent it becomes. Lua automatically rounds it when printing numbers, otherwise some outputs would be 0.00…01 instead of just 0. In your case, the number is so large that even whole numbers get affected.

r/facepalm right here because this is litteraly the hole point is to to what i am trying to do with numbers this large so why even say this if you know it won’t work for this big numbers

the problem is that there’s a value stored on the server that i need.

Other games deal with this by separating them into different values. Basically, 999,999,999 is a table of {999,999,999}. If the last number increases over 999, increase the previous one by 1.

Although you can practically use anything as the max individual values. You’re basically creating a number of a large base.

1 Like

@berezaa has a module called MoneyLib which has a function to shorten numbers like this.
I also made my own module that can also shorten numbers differently than berezaa’s. It shortens 1e+21 to 1.0Qn+ berezaa’s module would shorten 1e+21 to 1Qn. The link to money lib is here.

this would be useful if it wasn’t because the asset store says no items found when searching for MoneyLib

Guys, you don’t need to do anything you are suggesting :man_facepalming:

You can do operations on numbers of any size, you just can’t store them in a single variable. To solve this just save your number as a string.

To do an addition then on this you would do this:
Variable.Value = tonumber(Variable.Value) + Number

You can then send that string into whatever shortening method you have to display powers of 10 as Qn etc.

I updated my previous post to include my module and berezaa’s module. You can pick which one you like better. My module can only go up to a trillion so far, I can update it for you if you would like.

His issue is hitting a floating point error. It is large enough to make the number vary more than 10k. As for what you said with storing as a single variable, that doesn’t make any sense since the internal value is still a float. Stored float + another float is still a float, and given you are performing operations, the error will be even worse.

Yes, and what I said should fix that. Storing large numbers causes floating point errors, not doing operations on them.