在java中,我想做102%100并将余数作为02而不是2..我该怎么做?
在java中,我想做102%100并将余数作为02而不是2..我该怎么做?
int a = (102%100);
a 返回 2 而不是 02..我想得到 02.. 因为最初它应该是 1.02,但是 mod 返回 2。:( 有人知道该怎么做吗?
In java, I want to do 102%100 and get the remainder as 02 and not as 2.. How do i do that?
int a = (102%100);
a returns 2 and not 02.. I want to get 02..
because originally, it's supposed to be 1.02, but mod returns 2. :(
Anybody know how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
2
和02
是相同的数字。由于您使用它来查找小数点后的前两个数字,因此只需用
0
填充任何一位数即可。如果您想要小数点后的所有数字,更常用的方法是这样做:2
and02
are the same number.Since you are using this to find the first two numbers after the decimal-point, just pad any one-digit numbers with a
0
. If you want all the numbers after the decimal-point, the more usual way is to do this:NumberFormat 可能是最好的方法去。 String.format 和进行计算也可以,但由于您要格式化数字,因此最好使用数字格式化程序。
一个例子。我在这里猜测一下您的要求(例如 110%100 应该返回什么,或者您只期望个位数的余数?):
NumberFormat is probably the best way to go. String.format and doing calculations also work, but since you're formatting a number, might as well use a number formatter.
An example. I'm guessing on your requirements here a bit (e.g. what should 110%100 return, or do you only expect a single-digit remainder?):
作为整数,它们代表相同的数字。使用以下命令将数字格式化为字符串以供显示:
输出:
As integers, they represent the same number. Use the following to format the number as a string for display purposes:
Output:
下面是用零填充数字的答案:在 Java 中向数字添加前导零?
确实,不过如此。
Here lies the answer to padding numbers with zeroes: Add leading zeroes to number in Java?
Really though, thats it.