In PHP, you can get the first digit of an integer in the following ways:
- Using Regular Expression;
- Using
intdiv()
; - Using Floored Division;
- Reducing to Single Digit;
- Converting to String and Retrieving First Character.
#Using Regular Expression
You can use regular expression in the following way to get the first digit of an integer:
- Use
preg_match()
method with the\d
(or[0-9]
) pattern to get the first digit; - Convert numeric string back to integer;
- 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:
- Convert integer in absolute form to string and get length of the integer to determine the divisor;
- 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:
- Convert integer to absolute form;
- Get length of the integer to determine the divisor;
- Get the integer part from result of the division;
- 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:
- Convert integer to absolute form;
- Reduce number to single digit:
- If integer is greater than
10
, then keep dividing the number by10
till a number less than10
is left, or; - If integer is less than
10
, then return it as is as it's already a single digit number.
- If integer is greater than
- Get the integer part of the resulting decimal number;
- 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:
- Convert integer in absolute form to string;
- Get the first character;
- Convert the numeric character back to integer;
- 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
This post was published (and was last revised ) 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.