如何在java中用BigDecimal替换双精度数以表示货币
我正在做一项大学作业,我用双打来赚钱。我知道不建议使用双精度数作为货币值,所以我想知道如何用 BigDecimal 替换具有双精度数的现有代码。
由于这是一项大学作业,我不想对我的代码有直接帮助,也不想将其发布在这里,所以我创建了以下代码,我希望得到直接回复,即更改我提供的代码这样我就能明白发生了什么事。
我想知道 BigDecimal 如何与 int 或 double 相乘/除/加/减,或者这是否甚至不可能。
还有其他主题涵盖 BigDecimal 的用法,但它们没有涵盖我的具体问题,但是,如果我忽略了一篇已经得到解答的帖子,我深表歉意。
这是代码:
import java.util.Scanner;
import java.text.NumberFormat;
class example {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
NumberFormat fm = NumberFormat.getCurrencyInstance();
System.out.println("Enter the first amount:");
double money1 = in.nextDouble();
System.out.println("Enter the second amount:");
double money2 = in.nextDouble();
System.out.println("Enter the third amount:");
double money3 = in.nextDouble();
double tax = 0.2;
double totalmoney = money1 + money2 + money3;
double moneytax = (totalmoney)*tax;
System.out.println("Your money added together is: " + fm.format(totalmoney));
System.out.println("Tax on your money is: " + fm.format(moneytax));
System.out.println("Your money after tax deduction is: " + fm.format((totalmoney-moneytax)));
}
}
I am doing a college assignment where I have used doubles for money. I know it is not advisable to use doubles for monetary values so I am wondering how I can replace my existing code that has doubles with BigDecimal.
Since it is a college assignment, I didn't want direct help on my code and didn't want to post it here either, so I have created the following code to which I would appreciate direct replies, i.e. change the code I am providing so that I can understand what's going on.
I am wondering how BigDecimal can be multiplied/divided/added/subtracted with an int or a double or if that isn't even a possiblity.
There are other topics covering BigDecimal usage but they don't cover my specific question, however, I apologize if I have overlooked a post where it has already been answered.
Here's the code:
import java.util.Scanner;
import java.text.NumberFormat;
class example {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
NumberFormat fm = NumberFormat.getCurrencyInstance();
System.out.println("Enter the first amount:");
double money1 = in.nextDouble();
System.out.println("Enter the second amount:");
double money2 = in.nextDouble();
System.out.println("Enter the third amount:");
double money3 = in.nextDouble();
double tax = 0.2;
double totalmoney = money1 + money2 + money3;
double moneytax = (totalmoney)*tax;
System.out.println("Your money added together is: " + fm.format(totalmoney));
System.out.println("Tax on your money is: " + fm.format(moneytax));
System.out.println("Your money after tax deduction is: " + fm.format((totalmoney-moneytax)));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BigDecimal 的另一种选择可能是使用长整型以美分为单位进行所有计算,然后在末尾插入小数点。
An alternative to BigDecimal might be to do all calculations in cents, using longs, and just insert the decimal point right at the end.
如果你想避免使用 double,你需要从一开始就不要使用它。
您应该能够看到四舍五入后的结果完全相同。
If you want to avoid using double, you need to not use it from the start.
You should be able to see that the result is exactly the same after rounding.
BigDecimals 是不可变的,但您可以通过特定调用创建新的 BigDecimals:
您可能还想查看 scale() 方法。
BigDecimals are immutable, but you can create new ones with specific calls:
You'll probably also want to look at the scale() methods.