How To Get the First Digit of a Number in JavaScript

How To Get the First Digit of a Number in JavaScript? A number is a primitive data type, which is not an object and does not have any attributes or methods. Examples of number data types are real-life numbers like 59 and -420.  

In this article, we will study how to get the first digit of a number. The first digit refers to the first number when we look at the number from left to right. It also refers to the number on the highest decimal place. For example:- In 9856, 9 is the first digit. In -50.96, 5 is the first digit. Similarly, in 466.95, 4 is the first digit.

Get the First Digit Of A Number In JavaScript Using Iterative Approach

This method involves creating a loop and dividing the number by 10 until only a single digit is left with us. It is a much faster approach. Let’s have a look at the code and understand how it works.

function getFirstDigit(number) {
    // if number is negative than make it positive
    if(number < 0) {
        number *= -1;
    } 
    while (number >= 10) {
        number = number / 10;
        number = Math.floor(number)
    }
    return number
}
console.log(getFirstDigit(920))
console.log(getFirstDigit(547.36))
console.log(getFirstDigit(-850))
console.log(getFirstDigit(7))

Output:-

9
5
8
7

In the above code, we have created a function that has a loop in it to get the first digit of a number. If the number is negative then we have to convert it into a positive number, so that the negative sign doesn’t create any problems. The loop checks if the number is greater than or equal to 10, to start the iteration. If it is less than 10 it is obvious that it is a single-digit number and that number itself is at the first digit.

The goal of the loop is also the same i.e to convert any number to a single-digit number. In every iteration, the loop divides the number by 10 until a single digit is left. The Math.floor() function helps us get rid of the numbers after the decimal point. 

Get the First Digit Of A Number In JavaScript Using built-in Methods

This is another approach to getting the first digit of a number. It includes type conversion. Here also, If the number is negative then it will be converted to a positive number. The data type of the given number is converted and slicing is used to get the first digit. Let’s have a look at the code and then try to understand how it works.

function getFirstDigit(number) {
    // if number is negative than make it positive
    if (number < 0) {
        number *= -1;
    }
    const firstDigitStr = String(number)[0];
    const firstDigitInt = Number(firstDigitStr);
    return firstDigitInt
}
console.log(getFirstDigit(920))
console.log(getFirstDigit(547.36))
console.log(getFirstDigit(-850))
console.log(getFirstDigit(7))

Output:-

9
5
8
7

In the above function, we use the inbuilt wrapper method String to convert the data type of the number. The slicing operator with index 0 ( [ 0 ] ) is used to get the first character of a string. The first character which is extracted is then converted back to a number using the Number wrapper method.

Thus we have studied two methods to get the first digit of a number in Javascript. We hope these methods will help you out with your code. 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *