Best way to find how many digits in a number?

What is the best and most efficient way to find out how many digits are in a number? For an example the script would return a value of 6 if it were given this number: 368356.

I can’t seem to find any specific post regarding this. The best I found was how to return the specific digit number but not the total amount of digits. Any help is appreciated!

You can use logarithms.
math.ceil(math.log10(368356))

4 Likes

#tostring(num)

no reason to overcomplicate this

10 Likes

1.l Convert it to string the use the length operator (#): #tostring(value)
2. Use string.len, you don’t need tostring for this one: string.len(value)
3. Use logarithms then add one: math.log(math.floor(value), 10) + 1

math.log uses base 2.71828 by default. Use math.log10 or math.log(v, 10) (which is backported from Lua 5.2)

Edit: Values are converted to scientific notation for tostring, you can use #("%.0f"):format(value) in this instance.

4 Likes

That’s what I did last time, but it fails when tostring returns scientific notation.

1 Like