在Java中显示数字的前n位
当用户确定“n”时,我很难创建一种显示数字前 n 位的方法。
例如,用户输入整数“1234567”和若干位数以显示“3”。然后该方法输出“123”。
我有一个想法如何显示第一个数字:
long number = 52345678;
long prefix = number /= (int) (Math.pow(10.0, Math.floor(Math.log10(number))));
但我似乎无法弄清楚如何显示用户定义的前 n 位数字。
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想要一个号码:
If you want a number:
你可以这样做
如果你需要号码本身,你可以这样做
You could do this
If you need the number itself you can do