In JavaScript, you can get the first digit of an integer in the following ways:
- Using Regular Expression;
- Using
Math.trunc(); - 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:
- Convert integer to string and use
String.prototype.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(num) {
// 1: get first digit using regex pattern
const matches = String(num).match(/\d/);
// 2: convert matched item to integer
const digit = Number(matches[0]);
// 3: add sign back as needed
return (num < 0) ? -digit : digit;
}
console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
Using Math.trunc()
The following formula would give you the first digit of an integer:
quotient = trunc(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 of the resulting decimal number (using
Math.trunc()).
For example, you can implement this like so:
// ES6+
function firstDigit(num) {
// 1: convert absolute form to string and get length of integer
const len = String(Math.abs(num)).length;
const divisor = 10 ** (len - 1);
// 2: get integer part from result of division
return Math.trunc(num / divisor);
}
console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(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 of the resulting decimal number (using floored division);
- Add back the minus sign if the number was originally negative.
For example, you can implement this like so:
function firstDigit(num) {
// 1: convert to absolute form
const dividend = Math.abs(num);
// 2: get length of integer and determine divisor
const len = String(dividend).length;
const divisor = 10 ** (len - 1);
// 3: get integer part from result of division
const quotient = Math.floor(dividend / divisor);
// 4: add sign back as needed
return (num < 0) ? -quotient : quotient;
}
console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
Since the dividend is in absolute form, Math.floor() method works (similar to using Math.trunc()) for both, positive and negative, integers.
This works in the following way:
// num = -1234 // dividend = abs(-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 by10till a number less than10is 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:
// ES6+
function firstDigit(num) {
// 1: convert to absolute form
let absNum = Math.abs(num);
// 2: reduce number to single digit
while (absNum >= 10) {
absNum /= 10;
}
// 3: get integer part of fraction
absNum = Math.trunc(absNum);
// 4: add sign back as needed
return (num < 0) ? -absNum : absNum;
}
console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
Since the operand is in absolute form, you can also use the Math.floor() method to achieve the same (instead of using Math.trunc()).
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(num) {
// 1: convert absolute form to string
const numStr = String(Math.abs(num));
// 2: get first character
const firstChar = numStr[0];
// 3: convert back to integer
const firstDigitUnsigned = Number(firstChar);
// 4: add sign back as needed
return (num < 0) ? -firstDigitUnsigned : firstDigitUnsigned;
}
console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-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.