You can get the length of an integer in Ruby in any of the following ways:
- Converting to Array and Checking the
length
; - Converting to String and Checking the
length
; - Looping and Removing Digits Off the End;
- Calculating the Number of Digits.
#Converting to Array and Checking the length
If you have a positive integer, you can do the following:
- Convert integer to an array of digits (using
Integer#digits
); - Call the
Array#length
(orArray#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:
- Convert integer to string (using
Integer#to_s
); - 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:
- Create a loop, and remove the last digit from the number in each iteration till there are no digits left;
- 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
You should use this method with caution as it may give you the wrong result for really large numbers (such as 9999999999999999999999999
).
If you have a positive integer, you can do the following:
- Calculate the
log10
of the number, convert it to an integer and add1
to the result; - If the number is
0
, then return1
as a count (becauselog10(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
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.