How to Get the First Digit of an Integer in Ruby?

In Ruby, you can get the first digit of an integer in the following ways:

Using Integer#digits

You can use the Integer#digits method in the following way to get the first digit of an integer:

  1. Convert integer to absolute form;
  2. Use Integer#digits to convert the integer to a reversed array of digits (i.e. last digit appearing first and first digit appearing last in the resulting array);
  3. Access the last index of the array (which would have the first digit);
  4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

def first_digit(num)
  digit = num.abs.digits[-1]
  digit *= num.negative? ? -1 : 1
end

print first_digit(0) #=> 0
print first_digit(1234) #=> 1
print first_digit(-1234) #=> -1

Using Regular Expression

You can use regular expression in the following way to get the first digit of an integer:

  1. Convert integer to string and use String#slice(regexp) method with the \d (or [0-9]) pattern to get the first digit;
  2. Convert numeric string back to integer;
  3. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

def first_digit(num)
  digit = num.to_s[/\d/].to_i
  digit *= num.negative? ? -1 : 1
end

print first_digit(0) #=> 0
print first_digit(1234) #=> 1
print first_digit(-1234) #=> -1

Using Floored Division

You can use floored division (i.e. floor(x/y)) to get the first digit of an integer in the following steps:

  1. Convert integer to absolute form to get length of the integer;
  2. Get the quotient (which will be the first digit of the integer) — this is calculated as floor(int / 10 ** (intLength - 1));
  3. Convert the quotient (which would be a float) to integer;
  4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

def first_digit(num)
  len = num.abs.to_s.length
  divisor = 10 ** (len - 1)
  digit = num.abs.fdiv(divisor).to_i
  digit *= num.negative? ? -1 : 1
end

print first_digit(0) #=> 0
print first_digit(1234) #=> 1
print first_digit(-1234) #=> -1

This works in the following way:

# num = -1234
# len = 4
# divisor = 10 ^ (4 - 1) = 1000

# digit = floor(1234 / 1000)
# digit = floor(1.234)
# digit = 1
# digit = 1 * -1
# digit = -1

You can also use the Numeric#divmod method to achieve the same (instead of using Numeric#fdiv), for example, in the following way:

def first_digit(num)
  len = num.abs.to_s.length
  divisor = 10 ** (len - 1)
  digit, _ = num.abs.divmod(divisor)
  digit *= num.negative? ? -1 : 1
end

print first_digit(0) #=> 0
print first_digit(1234) #=> 1
print first_digit(-1234) #=> -1

Numeric#divmod returns an array with the quotient and the modulus, with the result already converted to integer.

Using String#slice

You can use the String#slice method to get the first digit of an integer in the following steps:

  1. Convert integer to absolute form;
  2. Convert the absolute value to string and get the first character;
  3. Convert sliced numeric character back to integer;
  4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

def first_digit(num)
  digit = num.abs.to_s[0].to_i
  digit *= num.negative? ? -1 : 1
end

print first_digit(0) #=> 0
print first_digit(1234) #=> 1
print first_digit(-1234) #=> -1

This is equivalent to using the String#slice method, for example, like so:

def first_digit(num)
  digit = num.abs.to_s.slice(0).to_i
  digit *= num.negative? ? -1 : 1
end

print first_digit(0) #=> 0
print first_digit(1234) #=> 1
print first_digit(-1234) #=> -1

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.