How to properly handle large numbers

Roblox’s IntValues can only seem to hold numbers smaller than 9223372036854775807
When converting a large number to a string, it will be shown via scientific notation (1e+n)

This is a problem I faced a long time ago, and I’m sure there are plenty of developers (both experienced and inexperienced) that will find themselves with the same problem.

How to solve it:

Use strings.
StringValues can hold 200,000 characters. I can’t exactly show you how big that really is, but here is something:

  • 1 billion = 1000000000 (10 characters)
  • 1 nonillion = 1000000000000000000000000000000 (31 characters)
    So, you can probably imagine how big a number of 200,000 characters is

How to use this in your code:

‘string numbers’ can be used in your code, by using the built-in tonumber() function.
This will convert any string into an integer, as long as it is able to do so.
(For instance, tonumber(“abc”) would not work, as ‘abc’ is not an integer)

Is this even useful?:

That’s debatable. In my opinion, yes! 200,000 characters worth of integer is a lot.
This is especially useful for simulators and other games that handle large amounts of numbers.

Before I finish this post, here is a number abbreviation function I made:

local function abbreviate(num)
    local suffixes = {"K","M","B","T","qd","Qn","sx","Sp","O","N","de","Ud","DD","tdD","qdD","QnD","sxD","SpD","OcD","NvD","Vgn","UVg","DVg","TVg","qtV","QnV","SeV","SPG","OVG","NVG"}
    local amnt = math.floor(((#num)-1)/3)
    local remove = 3*amnt
    if suffixes [amnt] then
    	return string.sub(tostring(num),1,(#num)-remove)..suffixes [amnt]
    else
    	return num
    end
end

With the suffixes I provided, it can go as far as 999NVG (93 characters!)
To use it, simply call the function, with the argument of a ‘string number’:
abbreviate("1000000") = “1M”

I hope some of you will find this useful. There are plenty of different ways to handle large numbers in your code, this is just one of them.

13 Likes

Disclaimer: don’t do this for stuff like currency. Lua numbers are doubles, which means that they lose precision after 2^53.

6 Likes

This may be the solution for you! https://github.com/berezaa/minershaven/blob/master/src/ReplicatedStorage/MoneyLib.lua

2 Likes

May I ask, why are you using IntValues in the first place? There’s only double in Luau and IntValue gets converted to double when using Luau scripts in it. There’s no reason to use IntValue over NumberValue. It still has the double-precision floating point limitations but only restricted from the minimum value of int64 to maximum value of int64. Thus this can only go up the 10 ** 308/2 ** 1024 even though StringValue can store string-like integer up to 10 ** 199.999.

And why are you posting a number abbreviation function as that’s unrelated to handling large number, and why should I use the function over other libraries like International?