Discord
Login
Community
DARK THEME

how do I find out the number of digits in a variable

i need a sprite to do increase its width based on the amount of digits in the variable

Assign this variable to another variable that way the value is checked. Or do you mean digits as in places?

a = 123
print( "a length = " + a.toString().length )

Will work well for positive integers.

getLength10 = function( value )
  result = 0
  value = abs( value )
  while value >= 1
    result += 1
    value = floor(value/10)
  end
  return result
end

Works for all numbers.

log10 = function( value )
  result = 0
  value = abs( value )
  if value != 0 then
    result = log(value)/log(10)
  end
  return floor(result) + 1
end

When you understand logarithm .

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community