如何在java中用BigDecimal替换双精度数以表示货币

发布于 2024-12-22 22:57:14 字数 1200 浏览 1 评论 0原文

我正在做一项大学作业,我用双打来赚钱。我知道不建议使用双精度数作为货币值,所以我想知道如何用 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 技术交流群。

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

发布评论

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

评论(3

千紇 2024-12-29 22:57:14

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.

眼泪也成诗 2024-12-29 22:57:14

如果你想避免使用 double,你需要从一开始就不要使用它。

BigDecimal money1 = new BigDecimal(in.nextLine());

您应该能够看到四舍五入后的结果完全相同。

If you want to avoid using double, you need to not use it from the start.

BigDecimal money1 = new BigDecimal(in.nextLine());

You should be able to see that the result is exactly the same after rounding.

数理化全能战士 2024-12-29 22:57:14

BigDecimals 是不可变的,但您可以通过特定调用创建新的 BigDecimals:

BigDecimal two = new BigDecimal("2.00");
BigDecimal four = new BigDecimal("4.00");

BigDecimal result = four.divide(two);

您可能还想查看 scale() 方法。

BigDecimals are immutable, but you can create new ones with specific calls:

BigDecimal two = new BigDecimal("2.00");
BigDecimal four = new BigDecimal("4.00");

BigDecimal result = four.divide(two);

You'll probably also want to look at the scale() methods.

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