Return first digit of an integer

后端 未结 23 1785
醉梦人生
醉梦人生 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 13:56

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

提交回复
热议问题