26

How in Java do you return the first digit of an integer.?

i.e.

345

Returns an int of 3.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Tony
  • 277
  • 1
  • 3
  • 3

25 Answers25

44

The easiest way would be to use String.valueOf(Math.abs((long)x)).charAt(0) - that will give it you as a char1. To get that as an integer value, you could just subtract '0' (as in Unicode, '0' to '9' are contiguous).

It's somewhat wasteful, of course. An alternative would just be to take the absolute value, then loop round dividing by 10 until the number is in the range 0-9. If this is homework, that's the answer I'd give. However, I'm not going to provide the code for it because I think it might be homework. However, if you provide comments and edit your answer to explain how you're doing and what problems you're running into, we may be able to help.


1One sticky point to note is that the absolute value of Integer.MIN_VALUE can't be represented as an int - so you may should first convert to a long, then use Math.abs, then do arithmetic. That's why there's a cast there.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    I don't like that this solution involves the conversion of an number to a string, but I do like the fact that its a oneliner, you got my vote. – monksy Jan 12 '10 at 19:44
  • 1
    That's going to be about '0' out. Doesn't work for Integer.MIN_VALUE. – Tom Hawtin - tackline Jan 12 '10 at 19:44
  • 2
    (1String.valueOf(-Math.abs(x)).charAt(1)` ? Or is that a bit obscure?) – Tom Hawtin - tackline Jan 12 '10 at 19:47
  • 1
    @Tom: I was just in the middle of editing for the Integer.MIN_VALUE issue. Working with longs makes it easier :) Not sure what you mean about it being '0' out though, given the second sentence of my first paragraph. It's not obvious whether the OP wants a character or an integer. – Jon Skeet Jan 12 '10 at 19:48
  • @Jon Skeet: You start off with "The easiest way" and then proceed to agglomerate 4 functions and 2 casts with instructions for further processing. It's not a bad solution per se but I cannot agree with the ridiculous number of upvotes you're getting for this byzantine contraption. – Carl Smotricz Jan 12 '10 at 20:01
  • 2
    @Carl: There's only a single cast (to `long`) there, and three method calls (`valueOf`, `abs`, `charAt`). The looping suggestion in the second form would still require a cast (or implicit conversion) and `abs` to cope with `Integer.MIN_VALUE`. I don't agree that it's byzantine, either - it seems perfectly natural to me. An integer really only has a "first digit" when you consider it in a formatted sense - so converting it to string and taking the first character is pretty obvious, isn't it? The other complexities are just due to coping with negative numbers. – Jon Skeet Jan 12 '10 at 20:06
  • @Jon Skeet: I miscounted casts, and the voters are obviously on your side. – Carl Smotricz Jan 12 '10 at 20:12
  • @Carl: That doesn't make the voters right, of course :) I certainly wouldn't want to put you off from criticising... which isn't the same as promising I won't defend my solutions :) (How many negatives can I get into a sentence?) – Jon Skeet Jan 12 '10 at 20:23
  • @Jon: That depends: How many function calls can you get into one line? ;) But seriously, I think you're working far too hard to catch corner cases in a poorly specified homework assignment. Still, it's a correct solution, so if you edit your answer I'll retract my vote. – Carl Smotricz Jan 12 '10 at 20:41
  • Another jab at your solution, though: It's wholly inappropriate as the solution to a homework problem. How is he ever going to explain that code to his teacher? ;) – Carl Smotricz Jan 12 '10 at 20:46
  • @Carl: Edited. Getting it working at all was for the OP. Working out the corner cases was for me :) – Jon Skeet Jan 12 '10 at 20:59
  • The other thing that you could do, assuming that we don't care about objects being created is `String.valueOf(BigInteger.valueOf(x).abs()).charAt(0)` – Paul Wagland Jan 12 '10 at 21:11
  • +1 - I am working on a library of String utils I use a lot, and am using this to convert numbers from int form to their 'name'. Ex. 9123 -> nine thousand one hundred twenty three. This will help a good bit. Thanks! By the way, yes, I did just use both of the things to avoid in this comment. – Nic Dec 22 '13 at 20:07
22
public static int firstDigit(int n) {
  while (n < -9 || 9 < n) n /= 10;
  return Math.abs(n);
}

Should handle negative numbers pretty well, too. Will return a negative first digit in that case.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • String generalSolution = carlsSolution.replace("345", "Math.abs(inputValue))"; – Andreas Dolk Jan 12 '10 at 19:49
  • @Andreas_D: I just realized I may have been missing votes because people couldn't make that intellectual leap, so I made an explicit function of it. – Carl Smotricz Jan 12 '10 at 19:54
  • 2
    @Gennadiy, I'm sorry I edited my solution since you made your comment, but it wasn't wrong before either. Any integer value whose square is greater than 81 has an absolute value greater than 9, and since it's an integer that means it's at least 10. – Carl Smotricz Jan 12 '10 at 19:56
  • @Tom: The spec doesn't say what to do with negative numbers. Are negative inputs allowed? Do they literally want the first *digit* or do they want the first *character* ( `-` ) ? I'm not willing to worry too hard about this. – Carl Smotricz Jan 12 '10 at 20:44
22

Yet another way:

public int firstDigit(int x) {
  if (x == 0) return 0;
  x = Math.abs(x);
  return (int) Math.floor(x / Math.pow(10, Math.floor(Math.log10(x))));
}
liwp
  • 6,746
  • 1
  • 27
  • 39
  • 3
    I'd be interesting to know whether that works correctly for every integer. I'd be concerned about floating point inaccuracies creeping in - but it may be okay. Hard to test efficiently... – Jon Skeet Jan 12 '10 at 19:55
  • Interestingly enough, this solution yields the same result as mine for all integers between 0 and 100,000,000. However, it doesn't do negatives. – Carl Smotricz Jan 12 '10 at 20:13
  • Needs slight alterations to return 0 if x == 0 and firstDigit(-x) if x < 0. – lins314159 Jan 12 '10 at 23:12
  • 1
    @lins314159: thanks, I added checks for those @Jon: I thought I'd test the algo with haskell's QuickCheck library and the results are interesting: when I run the test interactively it passes, when I compile the test code it fails at -1000, 1000. It turns out that log 1000 returns 2.999... in the compiled code, rather than 3. This is exactly the kind of floating point error you were worried about. This does not seem to happen in Java though (at least not with the same values). I'll see what the haskell mailinglist has to say. – liwp Jan 13 '10 at 10:15
  • divisor = (int)(Math.pow(10,(int) Math.log10(rem))); firstdigit = (int) (rem * 1.0 / divisor); – srikanth Nutigattu May 10 '20 at 18:18
8

Ignoring negative values leads to:

(""+345).charAt(0);
stacker
  • 68,052
  • 28
  • 140
  • 210
6

The missing recursive solution:

int getFirstInt(int input) {
  if (input > 0 ? input < 10 : input > -10) {
    return input > 0 ? input : -input;
  }
  return getFirstInt(input / 10);
}

I wouldn't use the ternary operator in real life but - isn't it kind of beautiful? ;)

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Doesn't cope with Integer.MIN_VALUE, but nice to see the recursive version. I don't think I'd use the conditional operator for the `if` condition, but I might use it for the return statement - at least if Math.abs didn't exist. – Jon Skeet Jan 12 '10 at 20:21
  • 1
    @Jon, tested it with Integer.MIN_VALUE before, and the result was '2', with and without casting to long... Is it really a problem? – Andreas Dolk Jan 12 '10 at 22:21
  • 2
    It won't be a problem since you never try to negate MIN_VALUE, but only ever try to negate values between -1 and -9, these (obviously) have positive equivalents. – Paul Wagland Jan 13 '10 at 09:44
5

I find this one more simpler:

int firstDigit(int num)
{
    if(num/10 == 0)
        return num;
    return firstDigit(num/10);

}
  • Input: 123 => Output: 1
  • Input: -123 => Output: -1
Aiden
  • 355
  • 5
  • 17
4

Updated: log10 solution:

A variation on the log10 solution with no division.:

public int getFirstDigit(int x) {
    double e = Math.log10(Math.abs((long) x));
    return Double.valueOf(Math.pow(10.0, e - Math.floor(e))).intValue());
}

What's it doing?

  1. cast the int to long (to handle the MIN_VALUE issue)
  2. get the absolute value
  3. calculate the log10
  4. calculate the floor of the log10
  5. subtract the floor from the log10 (the difference will be the fraction)
  6. raise ten to the difference, giving you the value of the first digit.

while loop solution:

To handle Integer.MIN_VALUE and keep Math.abs() and the cast to long out of the loop:

public static int getFirstDigit(int i) {
    i = Math.abs(i / (Math.abs((long)i) >= 10 ) ? 10 : 1);
    while (i >= 10 ) 
        i /= 10;
    return i;
}
Matthew Flynn
  • 2,210
  • 19
  • 27
2

Homework Hint: Convert it to a string and and return the first character.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
2

Fastest way would be :

  • Compute log of the abs(x), then get floor. Let's call it n.
  • Divide the number with 10^n
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • 3
    How certain are you that computing a log and computing a power and then dividing once will be faster than performing up to 9 integer divisions by 10? I wouldn't like to guess that, to be honest... or claim it to be universally true across all processors and JVMs. – Jon Skeet Jan 12 '10 at 20:22
  • 1
    I'm rather shocked about this, but it turns out that liwp's solution, which is very similar to this, returns the correct result over most of its range (I gave up after 100 million). Is that luck or are those transcendental functions very amenable to this exact problem, or what?? To be fair, +1 here too. – Carl Smotricz Jan 12 '10 at 21:43
  • 1
    As for performance... yeah. They probably won't run this algorithm at any supercomputing centers. Does it matter? ;) – Carl Smotricz Jan 12 '10 at 21:45
1

Looking at the code supplied it seems a tad over complicating the whole thing, here is a simple solution...

int number = 4085;
int firstDigit = number;
while (firstDigit > 9)
{
      firstDigit = firstDigit / 10;
}
System.out.println("The First Digit is " + firstDigit);
Ria
  • 10,237
  • 3
  • 33
  • 60
1
int main(void) {
  int num = 3421;

  while (num*num + 10 - num*(1 + num) <= 0) {
    num *= (num - 0.9*num)/num;
  }

  std::cout << num << std::endl;
}
Chip Uni
  • 7,454
  • 1
  • 22
  • 29
1
int a = 354;
int b = (int)(a / Math.Pow(10, (int)Math.Log10(a))); // 3
Terje
  • 1,753
  • 10
  • 13
1

Assume the number is int type

therefore,

int number = 352
// Change the int into String format
String numberString = Integer.toString(number);
// We can know the first digit of that String with substring method directly
Integer.parseInt(numberString.substring(0,1));

or the other way is to change the String into char and get the numerical value from char

e.g.

int number = 352;
String numberString = Integer.toString(number);
Character.getNumericValue(String.valueOf(target).charAt(0)); 
// 0 means the first digit of the number
0

I think this might be a nice way to do that:

public static int length(int number) {
    return (int)Math.log10(Math.abs(number)) + 1;
}

public static int digit(int number, int digit) {
    for (int i = 0; i < digit; i++) {
        number /= 10;
    }
    return Math.abs(number % 10);
}

Works for both negative and positive numbers. digit(345, length(345) - 1) will return 3, digit(345, 0) will return 5 e.g. and so on...

borchero
  • 5,562
  • 8
  • 46
  • 72
0
public static void firstDigit(int number){      
    while(number != 0){
        if (number < 10){
            System.out.println("The first digit is " + number);
        }
            number = number/10;

        }
    }

When you call it, you can use Maths.abs in order for it to work for negative number:

firstDigit(Math.abs(9584578)); 

This returns 9

alex
  • 11
  • 1
0

Its easier then any solution given hier if you only need the first digit/one sepcific digit:

int temp = 345%100 //temp = 45
int firstDigit = (345 -temp)/100 //fisrtDigit= 300/100 = 3

If you need to check the 2nd digit too then you need to repeat step one

 int tempHuns = 345%100 //tempHuns = 45  
 tempTens = tempHuns %10 // tempTens= 5
 int secondDigit = (tempHuns - tempTens)/10 //fisrtDigit= 40/10 = 4
resteqs
  • 19
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 21 '21 at 20:11
0

try this:

 public int getFirstDigit(int num){
    if (num < 0) {
        num = -num; // convert negative to positive
    }
    return Integer.parseInt(Integer.toString(num).substring(0, 1));
}
Saeid Mohammadi
  • 319
  • 1
  • 2
  • 11
-1

This is Groovy, but it should be easy to convert to Java:

int firstNum(int x) {
    a = Math.abs(x)
    sig = Math.floor(Math.log10(a))
    return a / Math.pow(10, sig)
}

Results:

groovy> println(firstNum(345))
3

groovy> println(firstNum(3452))
3

groovy> println(firstNum(-112))
1

groovy> println(firstNum(9999))
9

groovy> println(firstNum(Integer.MAX_VALUE))
2

groovy> println(firstNum(Integer.MIN_VALUE + 1))
2

matt b
  • 138,234
  • 66
  • 282
  • 345
-1

Here's an extremely simple way of obtaining the first and second digits of an integer separately, but this will only work for exactly two digits!

    int firstDigit = number / 10;
    int secondDigit = number % 10;

For a situation where the user might input more or less digits, but where you may not know how many, you could try this approach, but other answers have better solutions for this case. I just wrote this complete program up that you can paste into a compiler and run. Once you see the pattern you can check for as many digits as you want and then just have a catch for a length greater than you want to accept:

package testingdigits;

import java.util.Scanner;

public class TestingDigits {


Scanner keyboard = new Scanner(System.in);    

public static void main(String[] args) 
{

Scanner keyboard = new Scanner(System.in);

System.out.printf("\nEnter a number to test:");
int number = keyboard.nextInt();

int length = (int) Math.log10(number) + 1; //This gets the length of the number of digits used 

//Initializing variables first to prevent error
int firstDigit = 0, secondDigit = 0, thirdDigit = 0, fourthDigit = 0, fifthDigit = 0, sixthDigit = 0;

    if (length == 1)
     {
        firstDigit = number;
        System.out.println("" + firstDigit);
     }
    if (length == 2)
     {
        firstDigit = number / 10; //For example, 89/10 will output 8.9 and show as 8 in this case.
        secondDigit = number % 10;
        System.out.println("" + firstDigit + "" + secondDigit);
     }
    if (length == 3)
     {
        firstDigit = number / 10 / 10; // 123/10/10 is 1.23 and will show as 1
        secondDigit = number / 10 % 10;
        thirdDigit = number % 10;
        System.out.println("" + firstDigit + "" + secondDigit + "" + thirdDigit);
     }
    if (length == 4)
     {
        firstDigit = number / 10 / 10 / 10; 
        secondDigit = number / 10 / 10 % 10;
        thirdDigit = number / 10 % 10;
        fourthDigit = number % 10;
        System.out.println("" + firstDigit + "" + secondDigit + "" + thirdDigit + "" + fourthDigit);
     }
     if (length == 5)
     {
        firstDigit = number / 10 / 10 / 10 / 10; 
        secondDigit = number / 10 / 10 / 10 % 10;
        thirdDigit = number / 10 / 10 % 10;
        fourthDigit = number / 10 % 10;
        fifthDigit = number % 10;
        System.out.println("" + firstDigit + "" + secondDigit + "" + thirdDigit + "" + fourthDigit + "" + fifthDigit);
     }
    //You can probably see the pattern by now. It's not an elegant solution, but is very readable by even beginners:     
    if ((length == 6))
     {
        firstDigit = number / 10 / 10 / 10 / 10 / 10; //The first digit is divided by 10 for however many digits are after it
        secondDigit = number / 10 / 10 / 10 % 10;
        thirdDigit = number / 10 / 10 / 10 % 10;
        fourthDigit = number / 10 / 10 % 10;
        fifthDigit = number / 10 % 10; //The second to last always looks like this
        sixthDigit = number % 10;  //The last digit always looks like this no matter how big you go
        System.out.println("" + firstDigit + "" + secondDigit + "" + thirdDigit + "" + fourthDigit + "" + fifthDigit + "" + sixthDigit);
     }
    if ((length > 6))
     {
        //The main problem with this approach is that you have to define in advance how many digits you are working with
        //So it's simple, but not very elegant, and requires something to catch unexpected input from the user.
        System.out.println("Invalid Input!");
     }

  }

}

As a program it just outputs exactly the number you type in, but as you can see it's doing it in a way that shows that it's able to separate the digits of user input so you can use it as a test for a simple program, but again, it's not as good as some of the other solutions here, it's just a readable approach that works well for beginners. It also accepts negatives just fine.

Also: In a case where you start with a double you could use the following to convert to int and truncate decimals:

numberInt = (int)number;
Elliander
  • 503
  • 1
  • 9
  • 19
-1
//Try this one.
Scanner input = new Scanner(System.in);
System.out.println("enter first 9 digits: ");
String x = input.nextLine();
String x1 = x.substring(0,1);
int d1 = Integer.parseInt(x1);
System.out.println(d1);
// the substring gives the position of extraction. method dont seem to work for letters though
Garen
  • 7
  • 1
-1
int firstNumber(int x){
     int firstN = x;
     while(firstN > 9){
          firstN = (firstN - (firstN%10))/10;
     }
     return firstN;   
 }
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Torped
  • 1
-1

Here is a smaller version to get digits of all positions, it works with negative value (not decimal).

int number = -23456;

int length = (int) Math.log10(Math.abs(number)) + 1; //This gets the length of the number of digits used
//Math.abs to change negative int to positive

System.out.println("Digit at pos " + 1 + " is :- " + (int)(Math.abs(number)/Math.pow(10,(length-1))));

for (int i = 2; i <= length; i++){
    System.out.println("Digit at pos " + i + " is :- " + (int)(Math.abs(number)/Math.pow(10,(length-i))%10));
}
Ali Azam
  • 2,047
  • 1
  • 16
  • 25
-1

To separate digits of an integer from left to right I use 2 different methods, the first one to count how many digits the integer is made up of and then I split them from left to right by dividing the integer by 10 raised to the power of the number of digits minus 1.

//method to separate digits of an integer from left to right
private static void separateDigits(int num){
    int numOfDigits = countNumberOfDigits(num);
    for (int numOfZeros = numOfDigits-1; numOfZeros >= 0 ; --numOfZeros){
        int divisor = (int) Math.pow(10, numOfZeros);
        System.out.print( Math.abs(num) / divisor + " // " );
        num %= divisor;
    }
}

//method to count number of digits
private static int countNumberOfDigits(int num){
    int numOfDigits=0;
    //using absolute value of num allows method to work even with negative integers
    while(Math.abs(num) > 0){ 
        num = num / 10;
        numOfDigits++; //this counts the number of times the "while" loops
    }
    return numOfDigits;
}

No use of Arrays or recursive methods just simple division with "/" and "%".

Invoking the method:

public static void main(String args[]) {

separateDigits( -123456789 );

}

yields: 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8 // 9 //

-1

I think more simple to do :

int firstDigit = i-(i/10)*10 // i is an integer or long value, positive or negative.
Bruno L.
  • 457
  • 6
  • 8
-2

This way worked for me just fine, but it does involve converting from int to string and back to int.

Integer.parseInt(String.valueOf(x).substring(0,1));
The Halfling
  • 101
  • 2