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,它会给我11015,但没有便士。
我该如何解决这个问题?
发布于 2020-11-27 02:50:52
因为您将每次比较的结果赋给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) + " $ ");发布于 2020-11-27 03:19:58
您看到的问题是,为什么非常不鼓励使用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 $ 发布于 2021-07-13 05:48:05
您遇到的问题是浮点数表示不精确。
处理货币的最简单和最容易的方法是使用便士,它始终是一个整数,因此您可以使用int变量计算所有内容。
这就是银行处理购买交易数据的方式。
首先将用户输入转换为整数个便士:
double input = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the amount of Money", "Money Input", JOptionPane.QUESTION_MESSAGE));
int pennies = (int)(input * 100);然后,您的代码的其余部分需要转换为使用便士,这很简单。
https://stackoverflow.com/questions/65027453
复制相似问题