I am having difficulty of creating a method to display first n digits of a number when 'n' is determined by the user.
For example, user inputs an integer '1234567' and a number of digits to display '3'. The method then outputs '123'.
I have an idea how to display the first digit:
long number = 52345678;
long prefix = number /= (int) (Math.pow(10.0, Math.floor(Math.log10(number))));
But I seem not being able to figure out how to display a user defined first n digits.
Thank you!
解决方案
int a = 12345;
int n = 3;
System.out.println((""+a).substring(0, n));
If you want a number:
int b = Integer.parseInt((""+a).substring(0, n));
用户自定义显示数字:根据输入长度截取整数示例
4133

被折叠的 条评论
为什么被折叠?



