35

I am just learning Java and am trying to get my program to retrieve the first digit of a number - for example 543 should return 5, etc. I thought to convert to a string, but I am not sure how I can convert it back? Thanks for any help.

int number = 534;
String numberString = Integer.toString(number);
char firstLetterChar = numberString.charAt(0);
int firstDigit = ????
Michoel
  • 353
  • 1
  • 3
  • 4

9 Answers9

49

Almost certainly more efficient than using Strings:

int firstDigit(int x) {
    while (x > 9) {
        x /= 10;
    }
    return x;
}

(Works only for nonnegative integers.)

Sean
  • 29,130
  • 4
  • 80
  • 105
45
    int number = 534;
    int firstDigit = Integer.parseInt(Integer.toString(number).substring(0, 1));
Jordan Allan
  • 4,408
  • 7
  • 32
  • 35
  • 5
    this is honestly **so** much simpler/usable than all the esoteric/overly-complex and algorithmic answers in other places such as http://stackoverflow.com/questions/2051817/return-first-digit-of-an-integer -- maybe this won't scale in a mainframe scientific application with 20 digit numbers... but who's writing those anyway??? – Don Cheadle Feb 04 '15 at 19:31
28
firstDigit = number/((int)(pow(10,(int)log(number))));

This should get your first digit using math instead of strings.

In your example log(543) = 2.73 which casted to an int is 2. pow(10, 2) = 100 543/100 = 5.43 but since it's an int it gets truncated to 5

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Ben Burnett
  • 1,554
  • 10
  • 17
  • would be interesting to see how that benchmarks against string operations –  Jun 03 '10 at 16:39
  • 2
    looks like fun but are there any advantages to doing it this way? if i was reading that line and im not a maths professor i would have no idea what it does :-) – Michoel Jun 03 '10 at 16:40
  • Well, the log of 100 is 2 and the log of 999 is 2.99 so you can see that the log is returning the number of digits minus one if stored as an int. Number of digits tells you what to divide the number by. Then we use the built in power of int to truncate everything after the decimal leaving us with the first digit. I can't tell you which way is faster though. I just wanted to offer an alternative solution that doesn't require converting the number to a string. – Ben Burnett Jun 03 '10 at 16:43
  • 1
    Math.pow is so inefficient for ints though; you should roll your own there. – amara Jun 03 '10 at 16:58
  • readability is not archived. beside that you got one nice row :) – starcorn Jun 03 '10 at 17:09
  • 2
    It should be log10, as log is log base 2, in Math library. divisor = (int)(Math.pow(10,(int) Math.log10(rem))); leftdigit = (int) (rem * 1.0 / divisor); – srikanth Nutigattu May 10 '20 at 18:16
1
int firstDigit = Integer.parseInt(Character.toString(firstLetterChar));
Adamski
  • 54,009
  • 15
  • 113
  • 152
1
int number = 534;
String numberString = "" + number;
char firstLetterchar = numberString.charAt(0);
int firstDigit = Integer.parseInt("" + firstLetterChar);
Adam
  • 43,763
  • 16
  • 104
  • 144
  • 1
    is there a reason why you prefer to do numberString = "" + number instead of numberString = Integer.toString(number)? – Michoel Jun 03 '10 at 16:34
  • He is just lazy to type the whole thing :) – Dimitris Andreou Jun 03 '10 at 16:39
  • @Dimitris True. :) But "" + int = String is a Java idiom – Adam Jun 03 '10 at 16:46
  • 1
    And ""+int is a very clumsy idiom. It creates a StringBuilder, appends an empty string to it, converts the int to a string, appends this string to the StringBuilder, the converts the StringBuilder to a String. It's much more efficient to just say Integer.toString(n) or String.valueOf(n). – Jay Jun 03 '10 at 17:25
0

This example works for any double, not just positive integers and takes into account negative numbers or those less than one. For example, 0.000053 would return 5.

private static int getMostSignificantDigit(double value) {
    value = Math.abs(value);
    if (value == 0) return 0;
    while (value < 1) value *= 10;
    char firstChar = String.valueOf(value).charAt(0);
    return Integer.parseInt(firstChar + "");
}

To get the first digit, this sticks with String manipulation as it is far easier to read.

Brian Risk
  • 1,244
  • 13
  • 23
0

Integer.parseInt will take a string and return a int.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
0
int number = 534;
int firstDigit = number/100; 

( / ) operator in java divide the numbers without considering the reminder so when we divide 534 by 100 , it gives us (5) .

but if you want to get the last number , you can use (%) operator

    int lastDigit = number%10; 

which gives us the reminder of the division , so 534%10 , will yield the number 4 .

maysara
  • 5,873
  • 2
  • 23
  • 34
  • @Maysera, Adding code-only answer generally is considered bad quality post. It tells other what but not why this snippet solves the issue. Please to update your answer. – Maher Abuthraa Nov 05 '16 at 18:52
  • @MaherAbuthraa I've just did – maysara Nov 06 '16 at 22:22
  • 14
    Adding hard-coded 100 defeats the purpose, 543 is just an example. So, that doesn't work. It's like hard coding return 5. – srikanth Nutigattu May 10 '20 at 17:30
  • @srikanthNutigattu no it is not "hard coding". this works for all the numbers that are 3 digits. return 5 will not work for 666 :D – maysara May 10 '20 at 21:15
  • 1
    @MaysaraAlhindi Not sure why my previous comment is flagged about "hard coding" why was the assumption made that it is a 3 digit number? where OP hasn't expressed that. – srikanth Nutigattu Jun 24 '20 at 07:34
0

This way might makes more sense if you don't want to use str methods

int first = 1;
for (int i = 10; i < number; i *= 10) {
    first = number / i;
}
manfcas
  • 1,933
  • 7
  • 28
  • 47
Harvey
  • 11
  • 2