首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DecimalFormat部门

DecimalFormat部门
EN

Stack Overflow用户
提问于 2020-11-27 02:22:47
回答 3查看 66关注 0票数 1
代码语言:javascript
复制
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,但没有便士。

我该如何解决这个问题?

EN

回答 3

Stack Overflow用户

发布于 2020-11-27 02:50:52

因为您将每次比较的结果赋给mny变量,所以尝试不使用它们:

代码语言:javascript
复制
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) + " $ ");
票数 0
EN

Stack Overflow用户

发布于 2020-11-27 03:19:58

您看到的问题是,为什么非常不鼓励使用double来表示货币价值。请改用BigDecimal

代码语言:javascript
复制
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) + " $ ");
}

测试

代码语言:javascript
复制
printCoins(0.01);

printCoins(15.01);

输出

代码语言:javascript
复制
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 $ 
代码语言:javascript
复制
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 $ 
票数 0
EN

Stack Overflow用户

发布于 2021-07-13 05:48:05

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

处理货币的最简单和最容易的方法是使用便士,它始终是一个整数,因此您可以使用int变量计算所有内容。

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

首先将用户输入转换为整数个便士:

代码语言:javascript
复制
double input = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the amount of Money", "Money Input", JOptionPane.QUESTION_MESSAGE));
int pennies = (int)(input * 100);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65027453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档