检索数字的第一个数字 [英] Retrieving the first digit of a number
本文介绍了检索数字的第一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在学习Java,并且正在尝试让我的程序检索数字的第一个数字 - 例如543应该返回5,等等。我想转换为字符串,但我不知道如何转换回来了?感谢您的帮助。
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 = ????
推荐答案
int number = 534;
int firstDigit = Integer.parseInt(Integer.toString(number).substring(0, 1));
这篇关于检索数字的第一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文