How to Get the Length of an Integer in Ruby?

Find out how to determine the length of an integer in Ruby

You can get the length of an integer in Ruby in any of the following ways:

Converting to Array and Checking the length

If you have a positive integer, you can do the following:

  1. Convert integer to an array of digits (using Integer#digits);
  2. Call the Array#length (or Array#size) method on the resulting array of digits.
# Ruby 2.4+
print 12345.digits.length #=> 5

If the number can potentially be negative, then you must first get the absolute value of the integer:

# Ruby 2.4+
print -12345.abs.digits.length #=> 5

To make it reusable, you could make this into a function, for example, like so:

# Ruby 2.4+
def num_length(num)
    num.abs.digits.length
end

print num_length(0) #=> 1
print num_length(12345) #=> 5
print num_length(-12345) #=> 5
print num_length(9999999999999999999999999) #=> 25
print num_length(-9999999999999999999999999) #=> 25

Converting to String and Checking the length

If you have a positive integer, you can do the following:

  1. Convert integer to string (using Integer#to_s);
  2. Call the String#length method on the resulting string.
print 12345.to_s.length #=> 5

If the number can potentially be negative, then you must first get the absolute value of the integer:

print -12345.abs.to_s.length #=> 5

To make it reusable, you could make this into a function, for example, like so:

def num_length(num)
    num.abs.to_s.length
end

print num_length(0) #=> 1
print num_length(12345) #=> 5
print num_length(-12345) #=> 5
print num_length(9999999999999999999999999) #=> 25
print num_length(-9999999999999999999999999) #=> 25

Looping and Removing Digits Off the End

If you have a positive or a negative integer, you can do the following:

  1. Create a loop, and remove the last digit from the number in each iteration till there are no digits left;
  2. In each iteration, increment a counter, which would give the total number of digits in the number.
def num_length(num)
    len = 0
    loop do
        num = (num.abs / 10).to_i
        len += 1

        if num == 0
            break
        end
    end

    len
end

print num_length(0) #=> 1
print num_length(12345) #=> 5
print num_length(-12345) #=> 5
print num_length(9999999999999999999999999) #=> 25
print num_length(-9999999999999999999999999) #=> 25

Calculating the Number of Digits

If you have a positive integer, you can do the following:

  1. Calculate the log10 of the number, convert it to an integer and add 1 to the result;
  2. If the number is 0, then return 1 as a count (because log10(0) equals -Infinity).
num = 12345
print num == 0 ? 1 : Math.log10(num).to_i + 1 #=> 5

If the number can potentially be negative, then you must first get the absolute value of the integer:

num = -12345
print num == 0 ? 1 : Math.log10(num.abs).to_i + 1 #=> 5

To make it reusable, you could make this into a function, for example, like so:

def num_length(num)
    num == 0 ? 1 : Math.log10(num.abs).to_i + 1
end

print num_length(0) #=> 1
print num_length(12345) #=> 5
print num_length(-12345) #=> 5

// this may give wrong result for really large numbers
print num_length(9999999999999999999999999) #=> 26
print num_length(-9999999999999999999999999) #=> 26

You should use this method with caution as it may give you the wrong result for really large numbers (such as 9999999999999999999999999).


Hope you found this post useful. It was published . Please show your love and support by sharing this post.