How to Convert an Integer to an Array of Digits in Ruby?

In Ruby, you can convert an integer to an array of digits in the following ways:

Converting Numeric String to Array of Characters

If you are certain that the integer is always going to be positive, then you can simply do the following:

  1. Convert integer to numeric string;
  2. Convert numeric string to an array of characters (using String#chars);
  3. Use Array#map to convert each array item to an integer.

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer to string
    # 2: convert numeric string to array of chars
    # 3: convert each numeric string character to integer
    num.to_s.chars.map(&:to_i)
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(0) #=> [0]

However, if the integer can also be negative, then the following are some ways in which you can handle the negative sign (-) when converting an integer to an array of digits:

Make the Most Significant Digit Negative:

Making the most significant digit negative means that in the resulting array, if the original number is negative, the first digit will have the negative sign. This means that:

# 12345 becomes [1, 2, 3, 4, 5]
# -12345 becomes [-1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer in absolute form to string
    # 2: convert string to array of numeric string chars
    # 3: convert numeric string chars to integers
    digits = num.abs.to_s.chars.map(&:to_i)
    # 4: make first digit negative if original number is negative
    digits[0] = num < 0 ? -digits[0] : digits[0]

    digits
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(-12345) #=> [-1, 2, 3, 4, 5]

print num_to_array(0) #=> [0]
print num_to_array(-0) #=> [0]

Add the Minus Sign as a String:

Adding the minus sign (-) as a string means that in the resulting array, if the original number is negative, the first element of the array will have the minus sign as a string:

# 12345 becomes [1, 2, 3, 4, 5]
# -12345 becomes ['-', 1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer to string
    # 2: convert numeric string to array of chars
    digits = num.to_s.chars
    # 3: convert each numeric string character to integer
    digits.map { | curr_num |
        # 4: add '-' as is, if present
        (curr_num == "-") ? "-" : curr_num.to_i
    }
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(-12345) #=> ["-", 1, 2, 3, 4, 5]

print num_to_array(0) #=> [0]
print num_to_array(-0) #=> [0]

Add a Leading Digit to Represent the Sign

Adding a leading digit to represent the sign means that in the resulting array, the first element can be used to represent the sign (i.e. 0 for positive integers and 1 for negative integers):

# 12345 becomes [0, 1, 2, 3, 4, 5]
# -12345 becomes [1, 1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer in absolute form to string
    # 2: convert numeric string to array of chars
    # 3: convert each numeric string character to integer
    digits = num.abs.to_s.chars.map(&:to_i)

    # 4: add 0 or 1 as first element in array
    digits.unshift(num < 0 ? 1 : 0)

    digits
end

print num_to_array(12345) #=> [0, 1, 2, 3, 4, 5]
print num_to_array(-12345) #=> [1, 1, 2, 3, 4, 5]

print num_to_array(0) #=> [0, 0]
print num_to_array(-0) #=> [0, 0]

Reducing to Single Digit

If you are certain that the integer is always going to be positive, then you can simply do the following:

  1. Create a loop;
  2. Add the last digit (i.e. remainder of x % 10) as the first element to a new array;
  3. Remove the last digit from the integer in each iteration till there are no digits left.

For example, this can be implemented like so:

def num_to_array(num)
    digits = []

    # 1: create a loop
    loop do
        # 2: add last digit as first element in new array
        digits.unshift(num % 10)
        # 3: remove last digit from integer
        num /= 10

        # ...till there are no digits left
        if num == 0
          break
        end
    end

    digits
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(0) #=> [0]

However, if the integer can also be negative, then you can handle the negative sign (-) in the following ways:

Make the Most Significant Digit Negative:

Making the most significant digit negative means that in the resulting array, if the original number is negative, the first digit will have the negative sign. This means that:

# 12345 becomes [1, 2, 3, 4, 5]
# -12345 becomes [-1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer to absolute form
    abs_num = num.abs
    digits = []

    # 2: create a loop
    loop do
        # 3: add last digit as first element in new array
        digits.unshift(abs_num % 10)
        # 4: remove last digit from integer
        abs_num /= 10

        # ...till there are no digits left
        if abs_num == 0
          break
        end
    end

    # 5: make first digit negative if original number is negative
    if num < 0
        digits[0] = -digits[0]
    end

    digits
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(-12345) #=> [-1, 2, 3, 4, 5]

print num_to_array(0) #=> [0, 0]
print num_to_array(-0) #=> [0, 0]

Add the Minus Sign as a String:

Adding the minus sign (-) as a string means that in the resulting array, if the original number is negative, the first element of the array will have the minus sign as a string:

# 12345 becomes [1, 2, 3, 4, 5]
# -12345 becomes ['-', 1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer to absolute form
    abs_num = num.abs
    digits = []

    # 2: create a loop
    loop do
        # 3: add last digit as first element in new array
        digits.unshift(abs_num % 10)
        # 4: remove last digit from integer
        abs_num /= 10

        # ...till there are no digits left
        if abs_num == 0
          break
        end
    end

    # 5: add '-' to front of array
    if num < 0
        digits.unshift("-")
    end

    digits
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(-12345) #=> ["-", 1, 2, 3, 4, 5]

print num_to_array(0) #=> [0, 0]
print num_to_array(-0) #=> [0, 0]

Add a Leading Digit to Represent the Sign:

Adding a leading digit to represent the sign means that in the resulting array, the first element can be used to represent the sign (i.e. 0 for positive integers and 1 for negative integers):

# 12345 becomes [0, 1, 2, 3, 4, 5]
# -12345 becomes [1, 1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: convert integer to absolute form
    abs_num = num.abs
    digits = []

    # 2: create a loop
    loop do
        # 3: add last digit as first element in new array
        digits.unshift(abs_num % 10)
        # 4: remove last digit from integer
        abs_num /= 10

        # ...till there are no digits left
        if abs_num == 0
          break
        end
    end

    # 5: add 0 or 1 as first element in array
    leading_digit = (num < 0) ? 1 : 0
    digits.unshift(leading_digit)

    digits
end

print num_to_array(12345) #=> [0, 1, 2, 3, 4, 5]
print num_to_array(-12345) #=> [1, 1, 2, 3, 4, 5]

print num_to_array(0) #=> [0, 0]
print num_to_array(-0) #=> [0, 0]

Using Regular Expression

If you are certain that the integer is always going to be positive, then you can simply do the following:

  1. Use String#match with a regular expression to convert numeric string to an array of numeric string characters;
  2. Use Array#map to convert each array item to an integer.

For example, this can be implemented like so:

def num_to_array(num)
    # 1: use regex to match numbers
    # 2: convert each item to an integer
    num.to_s.to_enum(:scan, /\d/).map(&:to_i)
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(0) #=> [0]

However, if the integer can also be negative, then you can handle the negative sign (-) in the following ways:

Make the Most Significant Digit Negative:

Making the most significant digit negative means that in the resulting array, if the original number is negative, the first digit will have the negative sign:

# 12345 becomes [1, 2, 3, 4, 5]
# -12345 becomes [-1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: use regex to match '-' (optionally) and numbers
    # 2: convert each item to an integer
    num.to_s.to_enum(:scan, /-?\d/).map(&:to_i)
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(-12345) #=> [-1, 2, 3, 4, 5]

print num_to_array(0) #=> [0]
print num_to_array(-0) #=> [0]

Add the Minus Sign as a String:

Adding the minus sign (-) as a string means that in the resulting array, if the original number is negative, the first element of the array will have the minus sign as a string:

# 12345 becomes [1, 2, 3, 4, 5]
# -12345 becomes ['-', 1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: use regex to match numbers
    # 2: convert each item to an integer
    matches = num.to_s.to_enum(:scan, /\d/).map(&:to_i)

    # 3: add '-' to the front of array
    if num < 0
        matches.unshift("-")
    end

    matches
end

print num_to_array(12345) #=> [1, 2, 3, 4, 5]
print num_to_array(-12345) #=> ["-", 1, 2, 3, 4, 5]

print num_to_array(0) #=> [0, 0]
print num_to_array(-0) #=> [0, 0]

Add a Leading Digit to Represent the Sign:

Adding a leading digit to represent the sign means that in the resulting array, the first element can be used to represent the sign (i.e. 0 for positive integers and 1 for negative integers):

# 12345 becomes [0, 1, 2, 3, 4, 5]
# -12345 becomes [1, 1, 2, 3, 4, 5]

For example, this can be implemented like so:

def num_to_array(num)
    # 1: use regex to match numbers
    # 2: convert each item to an integer
    matches = num.to_s.to_enum(:scan, /\d/).map(&:to_i)
    # 3: add 0 or 1 as first element in array
    matches.unshift(num < 0 ? 1 : 0)

    matches
end

print num_to_array(12345) #=> [0, 1, 2, 3, 4, 5]
print num_to_array(-12345) #=> [1, 1, 2, 3, 4, 5]

print num_to_array(0) #=> [0, 0]
print num_to_array(-0) #=> [0, 0]

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.