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

Getting the first digit of a PHP integer

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

Using Regular Expression

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

  1. Use preg_match() 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:

function firstDigit(int $num): int
{
    // 1: get first digit using regex pattern
    preg_match('/\d/', $num, $matches);
    // 2: convert matched item to integer
    $digit = (int) $matches[0];
    // 3: add sign back as needed
    return ($num < 0) ? -$digit : $digit;
}

var_dump(firstDigit(1234)); // 1
var_dump(firstDigit(-1234)); // -1
var_dump(firstDigit(0)); // 0
var_dump(firstDigit(-0)); // 0

Using intdiv()

The following formula would give you the first digit of an integer:

quotient = intdiv(integer, integerLength - 1)

You can implement it in the following steps:

  1. Convert integer in absolute form to string and get length of the integer to determine the divisor;
  2. Get the integer part from result of the division (using intdiv()).

For example, you can implement this like so:

// PHP 7+
function firstDigit(int $num): int
{
    // 1: convert absolute form to string and get length of integer
    $len = strlen((string) abs($num));
    $divisor = 10 ** ($len - 1);
    // 2: get integer part from result of division
    return intdiv($num, $divisor);
}

var_dump(firstDigit(1234)); // 1
var_dump(firstDigit(-1234)); // -1
var_dump(firstDigit(0)); // 0
var_dump(firstDigit(-0)); // 0

This works in the following way:

// num = -1234

// len = 4
// divisor = 10 ^ (4 - 1) = 1000

// quotient = trunc(-1234 / 1000)
// quotient = trunc(-1.234)
// quotient = -1

Using Floored Division

The following formula would give you the first digit of an integer:

quotient = floor(abs(integer) / integerLength - 1)

You can implement it in the following steps:

  1. Convert integer to absolute form;
  2. Get length of the integer to determine the divisor;
  3. Get the integer part from result of the division;
  4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

function firstDigit(int $num): int
{
    // 1: convert to absolute form
    $dividend = abs($num);
    // 2: get length of integer and determine divisor
    $len = strlen((string) $dividend);
    $divisor = 10 ** ($len - 1);
    // 3: get integer part from result of division
    $quotient = floor($dividend / $divisor);
    // 4: add sign back as needed
    return ($num < 0) ? -$quotient : $quotient;
}

var_dump(firstDigit(1234)); // 1
var_dump(firstDigit(-1234)); // -1
var_dump(firstDigit(0)); // 0
var_dump(firstDigit(-0)); // 0

Since the dividend is in absolute form, floor() function works (similar to using intdiv()) for both, positive and negative, integers.

This works in the following way:

// num = -1234

// dividend = 1234
// len = 4
// divisor = 10 ^ (4 - 1) = 1000

// quotient = floor(1234 / 1000)
// quotient = floor(1.234)
// quotient = 1

// result = -1

Reducing to Single Digit

You can loop over the number and reduce it in each iteration till only a single digit (i.e. digit less than 10) is left. This can be done in the following steps:

  1. Convert integer to absolute form;
  2. Reduce number to single digit:
    • If integer is greater than 10, then keep dividing the number by 10 till a number less than 10 is left, or;
    • If integer is less than 10, then return it as is as it's already a single digit number.
  3. Get the integer part of the resulting decimal number;
  4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

function firstDigit(int $num): int
{
    // 1: convert to absolute form
    $absNum = abs($num);
    // 2: reduce number to single digit
    while ($absNum >= 10) {
        $absNum /= 10;
    }
    // 3: get integer part of decimal number
    // 4: add sign back as needed
    return (int) (($num < 0) ? -$absNum : $absNum);
}

var_dump(firstDigit(1234)); // 1
var_dump(firstDigit(-1234)); // -1
var_dump(firstDigit(0)); // 0
var_dump(firstDigit(-0)); // 0

Since the dividend is in absolute form, you can also use the floor() function to achieve the same (instead of using intval() or casting to integer).

Converting to String and Retrieving First Character

You can convert the integer to string and get the first digit in the following steps:

  1. Convert integer in absolute form to string;
  2. Get the first character;
  3. Convert the 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:

function firstDigit(int $num): int
{
    // 1: convert absolute form to string
    $numStr = (string) abs($num);
    // 2: get first character
    $firstChar = $numStr[0];
    // 3: convert back to integer
    $firstDigitUnsigned = (int) $firstChar;
    // 4: add sign back as needed
    return ($num < 0) ? -$firstDigitUnsigned : $firstDigitUnsigned;
}

var_dump(firstDigit(1234)); // 1
var_dump(firstDigit(-1234)); // -1
var_dump(firstDigit(0)); // 0
var_dump(firstDigit(-0)); // 0

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