如何在 Java 中有效使用模数运算符

发布于 2024-12-27 14:41:39 字数 762 浏览 1 评论 0原文

我正在用 Java 做一项与货币相关的大学作业。为此,建议我使用整数而不是双精度数,然后在打印报表时将其转换为美元值。

一切工作正常,直到我对数字 4005 进行计算(如 40.05 美元表示为 int)。我正在粘贴我遇到问题的代码部分,如果有人能告诉我我做错了什么,我将不胜感激。

import java.io.*;
class modumess {
    public static void main(String[] args) {
        int money = 4005; //Amount in cents, so $40.05;

        // Represent as normal currency
        System.out.printf("$%d.%d", money/100, money%100);
    }
}

上面的代码运行时显示 $40.5,而不是 $40.05。什么给?

请注意,这是我的作业,我想学习,所以我非常感谢这里对问题根源的解释,而不仅仅是一个简单的解决方案。

编辑:根据 Finbarr 的回答,我在代码中添加了以下内容,似乎已经解决了问题:

if (money%100 < 10) {
            format = "$%d.0%d";
        }

这是一个好方法吗?还是我在这里把事情过度复杂化了?

编辑:我只是想澄清一下,芬巴尔和韦斯的回答都对我有帮助,我接受了韦斯的回答,因为它让我更清楚如何继续。

I am doing a college assignment in Java that deals with currency. For that I am advised to use ints instead of doubles and then later convert it to a dollar value when I print out the statement.

Everything works fine until I do calculations on the number 4005 (as in $40.05 represented as an int). I am pasting the part of code I am having problems with, I would appreciate if someone could tell me what I am doing wrong.

import java.io.*;
class modumess {
    public static void main(String[] args) {
        int money = 4005; //Amount in cents, so $40.05;

        // Represent as normal currency
        System.out.printf("$%d.%d", money/100, money%100);
    }
}

The above code, when run, shows $40.5, instead of $40.05. What gives?

Kindly note that this is for my homework and I want to learn, so I would really appreciate an explanation about the root of the problem here rather than just a simple solution.

EDIT: Following Finbarr's answer, I have added the following to the code which seems to have fixed the problem:

if (money%100 < 10) {
            format = "$%d.0%d";
        }

Is this a good way to do it or am I over-complicating things here?

EDIT: I just want to make it clear that it was both Finbarr and Wes's answer that helped me, I accepted Wes's answer because it made it clearer for me on how to proceed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

祁梦 2025-01-03 14:41:39

对于一般情况,更好的方法是这样的:

format = "%d.%02d";

%02d 为 2 位数字提供 0 填充。这样你就不需要额外的 if 语句。

有关可以使用格式执行的操作的更多说明,请参阅此内容: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

A better way would be something like this for a general case:

format = "%d.%02d";

%02d gives you 0 padding for 2 digits. That way you don't need the extra if statement.

See this for more explanation of things you can do in format: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

〆凄凉。 2025-01-03 14:41:39

模运算符返回除法后的余数,而不进行小数计算。在本例中,4005%100 返回 5,因为 4005/100 的余数为 5。

The modulus operator returns the remainder after division without fractional calculation. In this case, 4005%100 returns 5 as the remainder of 4005/100 is 5.

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