How to remove the scientific notation when converting string into a number

local stringNumber = "921028090102102802201601203203102205070150101703103903602102805019031012010120901302201014031020503303109150501403102401070270160120310190103102023021028012050330"
local realNumber = tonumber(stringNumber)
print(realNumber) -- prints out 9.210280901021028e+161

How can I keep the number?

1 Like

Just print the stringNumber instead of printing the realNumber, since you have quotations it will print as a string, but since its just a number, you dont need the quotations.

Also to add on, you will have an overflow of data with this big of a number- so it wont be precise and instead use scientific notation to abbreviate.

1 Like
print(string.format("%.0f", number))

should do just that.
This does not work if you number has decimals though

3 Likes

ah u learn something new every day

2 Likes

I’ve already tried that and there are 2 reasons it doesn’t work.

  1. It still stays as a string. I need it as an actual number.
  2. I need the exact number and string.format("%.0f", number) changes some of the numbers.
1 Like

Yes i’ve seen it loses some precision. Even using tonumber causes it to already loose precision

local str = "12345678901234567890"
local num = tonumber(str)
print(str, num) --> 12345678901234567890 12345678901234567000
                                    ^^^^                 ^^^^

I just don’t think lua is capable of reliably handling these big numbers.

2 Likes

Its impossible for any computer to compute such high numbers with absolute precision. @YT0674

Read up on Binary to know why

2 Likes

local stringNumber = “921028090102102802201601203203102205070150101703103903602102805019031012010120901302201014031020503303109150501403102401070270160120310190103102023021028012050330”

This Is not a number and if it was it would be wrote as 9.210280901021028e+220.
You may have problems reading that but the program will not.

You can’t keep this a number as it never was a number…

In number form it is so big the program has to use a formula to show it, aka; Scientific Notation. Which is basically the number showen multiplied by the power of 10, X amount of times.

So that number really isn’t a 9.210… that is part of the formula to the real number, the latter being e+220. Meaning you can’t just format the string to get the real number.

There is nothing wrong with the program’s or Roblox’s logic …

5 Likes

Lua stores all number values as floats, and floats do not have the capability to store this number at full precision. You will either have to sacrifice the least significant digits or store it as a string.

There are Lua libraries that allow for arbitrary precision integer representation manipulation. Here is one of many.

1 Like

Thank you guys for your explanations! That library would come in really handy. Only problem is that I never did something like this. I have no idea how to use it.

1 Like

Not sure what you’re really doing … So here is a wild guess.
A little math trick workaround, to address floating-point precision errors.

function SnapToGrid(value, gridSize)
    local correctedValue = math.round(value / gridSize) * gridSize
    return correctedValue + 0.0000000001
end

this: + 0.0000000001 somehow fixes it.

function CorrectPrecision(value, precision)
    local factor = 10^precision
    return math.round(value * factor) / factor + 0.0000000001
end

--use:
local position = 1234567.123456789
local corrected = CorrectPrecision(position, 6) 
print(corrected) -- Outputs: 1234567.123457

I guess this is a way to keep the number within reach for Roblox with the least amount of being off. At that point is like a planck length… :rofl:

Not sure what you’re really doing

I’m trying to store letters inside an Ordered Data Store. I made a custom letter to number converter for it. That’s why I need the exact number. Otherwise some letters will be different.

Thank you for trying to help me! :smile:

1 Like

Sweet, I love encryption… How did you ever end up with a number that big out of 256 chars.
Going Unicode on us? :rofl:

function LetterToNumber(letter)
	return string.byte(letter)
end

function StringToNumber(text)
	local result = {}
	for i = 1, #text do
		table.insert(result, LetterToNumber(text:sub(i, i)))
	end
	return table.concat(result, ",")
end

function NumberToString(encodedText)
	local numbers = string.split(encodedText, ",")
	local text = ""
	for _, num in ipairs(numbers) do
		text = text .. string.char(tonumber(num))
	end
	return text
end

local text = "You have got to test everything before you post it and you know this!"
local encoded = StringToNumber(text)
print(encoded)

local decoded = NumberToString(encoded)
print(decoded)
1 Like

I’m trying to save bio’s that people made which contains a lot of letters. I tried typing a longer text into your work and I got the scientific notation again with an error to it.

My bad… That’s what I get for not testing first. Should work now.
This converts the letters to their char values. Basically the 1st step to encryption. What this needs now is a variant. Like say 22, with that you could add 22 to each outcome for each letter. Then subtract 22 from each letter outcome when decrypting. All in all this is rather weak encryption but, It will stop 99% from figuring it out. Perfect for your purpose. Or leave it as is if you don’t wish to encrypt other than the number conversion…

I did a bit of research on encryption in Roblox … I’m not sure if encryption would break terms of service or not. You might want to check before using this.