Return first digit of an integer

后端 未结 23 1783
醉梦人生
醉梦人生 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:10

    int firstNumber(int x){
         int firstN = x;
         while(firstN > 9){
              firstN = (firstN - (firstN%10))/10;
         }
         return firstN;   
     }
    

提交回复
热议问题