在java中,我想做102%100并将余数作为02而不是2..我该怎么做?

发布于 2024-12-20 23:12:05 字数 146 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你没皮卡萌 2024-12-27 23:12:05

202 是相同的数字。

由于您使用它来查找小数点后的前两个数字,因此只需用 0 填充任何一位数即可。如果您想要小数点后的所有数字,更常用的方法是这样做:

//Strip all numbers before decimal-point
double decimalDigits = myNumber - (int)myNumber;

2 and 02 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:

//Strip all numbers before decimal-point
double decimalDigits = myNumber - (int)myNumber;
若无相欠,怎会相见 2024-12-27 23:12:05

NumberFormat 可能是最好的方法去。 String.format 和进行计算也可以,但由于您要格式化数字,因此最好使用数字格式化程序。

一个例子。我在这里猜测一下您的要求(例如 110%100 应该返回什么,或者您只期望个位数的余数?):

NumberFormat formatter = new DecimalFormat("'0'#");
int remainder = 102%100;
System.out.println(formatter.format(remainder));

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?):

NumberFormat formatter = new DecimalFormat("'0'#");
int remainder = 102%100;
System.out.println(formatter.format(remainder));
风筝在阴天搁浅。 2024-12-27 23:12:05

作为整数,它们代表相同的数字。使用以下命令将数字格式化为字符串以供显示:

int a = 102 / 100;
int r = 102 % 100;
System.out.print(a + "." + String.format("%02d", r));

输出:

1.02

As integers, they represent the same number. Use the following to format the number as a string for display purposes:

int a = 102 / 100;
int r = 102 % 100;
System.out.print(a + "." + String.format("%02d", r));

Output:

1.02
完美的未来在梦里 2024-12-27 23:12:05

下面是用零填充数字的答案:在 Java 中向数字添加前导零?

确实,不过如此。

Here lies the answer to padding numbers with zeroes: Add leading zeroes to number in Java?

Really though, thats it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文