Return first digit of an integer

后端 未结 23 1786
醉梦人生
醉梦人生 2020-11-28 13:12

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

i.e.

345

Returns an int of 3.

23条回答
  •  一生所求
    2020-11-28 14:02

    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);
    

提交回复
热议问题