Possible to change string format depending on value?

I’ve looked around the dev forum and haven’t been able to find what I’ve been looking for so I decided to make my own post.
Is it possible to change how long a value can be dependent on the length of it?
The reason I am using String.Format is so that it doesn’t appear in scientific notion as I’m using very small decimal numbers if there is another way that’s easier I would also like to know.

Here’s part of the code there’s a comment where Im asking the question

for rankInLB, dataStored in ipairs(KillsPage) do
				
				--local thing = dataStored.value /10000000
				--if dataStored.value /10000000 < 0.000000000 then thing = string.format("%.15f",dataStored.value /10000000) end
				local number = string.len(dataStored.value)
				
				local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
				local Kills = dataStored.value /10000000000	
				local template = script.Template:Clone()
				template.Name = name .. "Leaderboard"
				template.PlrName.Text = name	
				template.Rank.Text = "#" .. rankInLB		
				template.Kills.Text =  string("%.xxxxf",Kills)-- I want to replace xxxx here with the value of number			
				template.Parent = script.Parent				
			end		
2 Likes

Can you give an example of what you mean by this?

2 Likes

with string.format I can limit the length of a number

and example is like this: string.format(“%.5f”, 0.11111111111111111111111111111)
that would limit the number 0.11111111111111111111111111111 to the fifth decimal place or 0.11111

what I want to do is make it so that I can find the length of the number and replace the 5 that I used with how ever long the number is. The reason for this is so that it doesn’t appear in scientific notation.

Im pretty much just trying to find the equivalent of doing “%0.f” but for a decimal instead of an integer

2 Likes

A number could be infinitely long though. %f is supposed to do what you’re asking, is it giving you scientific?

1 Like

the numbers im using are never infinitely long I just need a way to show the number without scientific notion
Edit: wait let me test something ill come back to you after

1 Like

ok what your saying does work by showing everything but there Is an infinite amount of 0’s shown after the number.

I want the %.f adjust to the length of the number so it only shows what it has to ex:

0.000007 → 0.000007 NOT 0.000007000000 and NOT 0

0.4 → 0.4 NOT 0.400000000 and NOT 0

1 Like

You might have to pick some max length then remove the trailing zeroes manually with a for loop or a gsub.

1 Like

Yeah this one works:

s = string.gsub(stringWithDaTrailingZeroes, "[0]*$", "")
1 Like

maybe I put it in my code wrong but with it the input is a million times larger than its supposed to be
can you show me how I would put it in the code I showed in the original topic incase I did it wrong?

1 Like

Works like this:

1 Like

when the number is very small it prints 0
image
image

1 Like

after some tinkering I found out you were right it’s just that %f isn’t infinite and instead only goes up to about 7 decimal places. I fixed it by putting %.20f instead and then removing the 0’s with gsub. thanks for your help.

before I mark you as the solution I was wondering if it was possible to remove the . at the end of numbers that don’t have a decimal place?

1 Like

it might be but I don’t know how i’d do it with gsub.

1 Like

nvm it doesn’t matter thanks for all your help!

In string pattern matching, . is a “magic character” that represents a character class that contains all characters. When you want to search for . itself, you can escape it using %.

local noTrailingZeroesOrUselessPoint = string.gsub(formattedNumber, "%.0*$", "")

for anyone in the future looking at this forum I found a better solution that also gets rid of any pesty little 00000000000000001’s or 99999999999998’s that you might have at the end of your code.

if number < 1 then
				local thing = number
				while thing < 1 do
					thing = thing*10
					f = f + 1
				end
			end

local Kills = string.format(“%.”… f…“f”, number)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.