小数格式除法


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) + " $ ");

如果我输入0.01,它给我1便士,但如果我输入15.01,它给了我1 10 1 5,但没有便士
我应该如何解决这个问题?

您看到的问题是,为什么强烈建议使用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 $ 

您遇到的问题是浮点数字表示不精确。

处理货币的最简单方法是使用便士,因为便士总是一个整数,所以可以使用int变量计算所有内容。

这就是银行处理采购交易数据的方式。

从将用户输入转换为整数便士开始:

double input = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the amount of Money", "Money Input", JOptionPane.QUESTION_MESSAGE));
int pennies = (int)(input * 100);

然后,您的代码的其余部分需要转换为使用便士,这很简单。

因为您要将每次比较的结果分配给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) + " $ ");