如何在Java中正确返回money double变量

时间:2014-01-27 20:19:18

标签: java variables currency

我目前正在为学校做作业,我需要编写一个程序,需要在此模式下返回您的更改

输入: purchaseAmount:12.45美元 givenAmount:$ 15.00

输出: 变化:2.55美元 2美元钞票 2个季度 1镍

但是,如果我使用这种情况,它可以正常工作。现在,如果我希望这个场景能够解决这个问题

输入: purchaseAmount:19.51美元 givenAmount:50.01美元

输出:

二十美元的钞票1 十美元钞票:1 宿舍:1 尺寸:2 便士:4

这是不正确的,因为我回到客户30.49而不是30.5

无法弄清楚这里的问题是什么。

由于

 boolean isFiveBillOn = false, isTwentybillOn = false, isTenbillOn = false, isDollarOn = false, isQuartersOn = false, isDimesOn = false, isNicklesOn = false, isPenniesOn = false;

    //retrieve amount due
    String moneyd = JOptionPane.showInputDialog("Enter the amount due");
    double amountd = Double.parseDouble(moneyd);

    String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");
    double amountr = Double.parseDouble(moneyr);

    //calculate difference
    double difference = (amountr - amountd);

    //convert to pennies
    int balance = (int)(difference * 100);

    //twenty dollar bill
    int twentydollars = balance / 2000;
    balance = (balance % 2000);
    if (twentydollars > 0) {
        isTwentybillOn = true;
    }

    //ten dollar bill
    int tendollars = balance / 1000;
    balance = (balance % 1000);
    if (tendollars > 0) {
        isTenbillOn = true;
    }

    //five dollar bill
    int fivedollars = balance / 500;
    balance = (balance % 500);
    if (fivedollars > 0) {
        isFiveBillOn = true;
    }

    //dollar bill
    int dollars = balance / 100;
    balance = (balance % 100);
    if (dollars > 0) {
        isDollarOn = true;
    }

    //quartes
    int quarters = balance / 25;
    balance = (balance % 25);
    if (quarters > 0) {
        isQuartersOn = true;
    }

    //dimes
    int dimes = balance / 10;
    balance = (balance % 10);
    if (dimes > 0) {
        isDimesOn = true;
    }

    //nickles
    int nickles = balance / 5;
    balance = (balance % 5);
    if (nickles > 0){
        isNicklesOn = true;
    }

    //pennies
    int pennies = balance / 1;
    balance = (balance % 1);
    if (pennies > 0){
        isPenniesOn = true;
    }

    //checking for difference over 0
    if (difference < 0) {
        JOptionPane.showMessageDialog(null, "The amount received can't be less than the amount owned");
    } else {
        // Returning Message
        } if (isTwentybillOn) {
                    System.out.println("Twenty dollar bill " + twentydollars);
        } if (isTenbillOn) {
                    System.out.println("Ten dollar bill: " + tendollars);
        } if (isDollarOn) {
                    System.out.println("One Dollar Bill: " + dollars);
        } if (isQuartersOn) {
                    System.out.println("Quarters: " + quarters);
        } if (isDimesOn) {
                    System.out.println("Dimes: " + dimes);
        } if (isNicklesOn) {
                    System.out.println("Nickles: " + nickles);
        } if (isPenniesOn) {
                    System.out.println("Pennies: " + pennies);
        } else {
            System.out.println("Thanks! have a good day");
        }
    }

1 个答案:

答案 0 :(得分:4)

您永远不应使用double或任何其他浮点数据类型来代表金钱。原因是它们不能完全代表所有值。例如,以下行:

System.out.println(new BigDecimal(0.1));

打印

0.1000000000000000055511151231257827021181583404541015625

如需更长时间的解释原因,请参阅What Every Computer Scientist Should Know About Floating-Point Arithmetic

我建议改用BigDecimal

如果您只想使用基元,则可以使用int来代替以美分存储货币值,并在显示值时除以100。