decimalformat除法

72qzrwbm  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(470)
double mny = 0;
        mny = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the amount of Money", "Money Input", JOptionPane.QUESTION_MESSAGE));
       //Check for Tens
                System.out.println("The # of Tens is " + (int)(mny/10));
                mny = mny%10;
                System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny) + " $ ");
        //Check for Fives
                System.out.println("The # of Fives is " + (int)(mny/5));
                mny = mny%5;
                System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny) + " $ ");
        //Check for Pennies
                System.out.println("The # of Pennies is " + (int)(mny/0.01));
                mny = mny%0.01;
                System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny) + " $ ");
    }//end class

如果我输入0.01,它会给我1便士,但是如果我输入15.01,它会给我1 10 1 5便士,但是没有便士,我该怎么解决这个问题呢?

6mw9ycah

6mw9ycah1#

你所看到的问题是为什么它是非常不鼓励使用 double 货币价值。使用 BigDecimal 相反。

public static void printCoins(double mny) {
    BigDecimal amount = BigDecimal.valueOf(mny);

    //Check for Tens
    BigDecimal[] quotientAndRemainder = amount.divideAndRemainder(BigDecimal.TEN);
    BigDecimal tens = quotientAndRemainder[0].setScale(0);
    amount = quotientAndRemainder[1];
    System.out.println("The # of Tens is " + tens);
    System.out.println("The remaining amount of money is " + amount.setScale(2, RoundingMode.HALF_EVEN) + " $ ");

    //Check for Fives
    quotientAndRemainder = amount.divideAndRemainder(BigDecimal.valueOf(5));
    BigDecimal fives = quotientAndRemainder[0].setScale(0);
    amount = quotientAndRemainder[1];
    System.out.println("The # of Fives is " + fives);
    System.out.println("The remaining amount of money is " + amount.setScale(2, RoundingMode.HALF_EVEN) + " $ ");

    //Check for Pennies
    quotientAndRemainder = amount.divideAndRemainder(BigDecimal.valueOf(0.01));
    BigDecimal pennies = quotientAndRemainder[0].setScale(0);
    amount = quotientAndRemainder[1];
    System.out.println("The # of Pennies is " + pennies);
    System.out.println("The remaining amount of money is " + amount.setScale(2, RoundingMode.HALF_EVEN) + " $ ");
}

测验

printCoins(0.01);

printCoins(15.01);

输出

The # of Tens is 0
The remaining amount of money is 0.01 $ 
The # of Fives is 0
The remaining amount of money is 0.01 $ 
The # of Pennies is 1
The remaining amount of money is 0.00 $
The # of Tens is 1
The remaining amount of money is 5.01 $ 
The # of Fives is 1
The remaining amount of money is 0.01 $ 
The # of Pennies is 1
The remaining amount of money is 0.00 $
qfe3c7zg

qfe3c7zg2#

因为您要将每个比较的结果分配给 mny 变量,请尝试不使用它们:

double mny = 0;
mny = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the amount of Money", "Money Input", JOptionPane.QUESTION_MESSAGE));

//Check for Tens
System.out.println("The # of Tens is " + (int)(mny/10));
System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny % 10) + " $ ");

//Check for Fives
System.out.println("The # of Fives is " + (int)(mny/5));
System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny % 5) + " $ ");

//Check for Pennies
System.out.println("The # of Pennies is " + (int)(mny/0.01));
System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny % 0.01) + " $ ");

相关问题