我正在创建一个回馈变更的系统。当我运行程序时,我得到: (假设我给了100美元)
You have given 100.0 dollars.
Your change is 14.670000000000002 dollars.
You will recieve:
0.6700000000000017 dollars
0.1700000000000017 quarters
0.0700000000000017 dimes
0.020000000000001697 nickels
1.6965595595053173E-15 pennies
码
import java.util.Scanner;
public class RunGivingChange {
public static void main(String[] args) {
// TODO Auto-generated method stub
double given;
double cost = 85.33;
double change;
int dollars;
int quarters;
int dimes;
int nickels;
int pennies;
Scanner input = new Scanner(System.in);
System.out.println("Your total is " + cost + " dollars.");
System.out.println("How much money are you giving?");
given = input.nextDouble();
if (given >= cost)
{
System.out.println("You have given " + given + " dollars.");
change = given - cost;
System.out.println("Your change is " + change + " dollars.");
System.out.println("You will recieve:");
//dollars
change = change % 1;
System.out.println(change + " dollars");
//quarters
change = change % 0.25;
System.out.println(change + " quarters");
//dimes
change = change % 0.10;
System.out.println(change + " dimes");
//nickels
change = change % 0.05;
System.out.println(change + " nickels");
//pennies
change = change % 0.01;
System.out.println(change + " pennies");
}
else if (given < cost)
{
System.out.println("Not enough money.");
}
else
{
System.out.println("Error.");
}
}
}
答案 0 :(得分:3)
您使用双精度浮点值来表示金钱。
不要这样做。
浮点数可以为您提供大约15个有效精度数字,但具有惊人的范围(和计算性能),这使得它们可用于科学用途。
使用固定点类型或BigDecimal作为货币值。
答案 1 :(得分:0)
模数(%)并不像你期望的那样工作(除了精度问题):
2.67 % 1
不会导致2
,但会导致0.67
。
(有关更多信息,请查看此处:How do I use modulus for float/double?)
您可能想要计算Math.floor( 2.67 / 1)
等并从change
中减去舍入值。
或者,如果change
始终为正(应该是),则可以将(并因此剪切)转换为整数:
int dollars = (int)(change / 1);
change -= dollars;
int quarters = (int)(change / 0.25);
change -= 0.25 * quarters;
...
要克服这些前提问题,请使用BigDecimal或将所有值转换为便士,即更改= 1467便士,1美元= 100便士等。
答案 2 :(得分:0)
而不是以美元为代价,你可以用最小的部门(便士)来表达:
double cost = 85.33;// dollars
you could express it as :
int cost = 8533: .. //pennies
然后,您可以使用/
和%
操作序列来计算每个货币分配。
例如:
int dollars = change / 100; // how many dollars fit in that pennies amount
change = change % 100; // how much pennies are left
//repeat for the other subdivisions
答案 3 :(得分:0)
如果您使用的是双打,请确保舍入到小数位。
change = (double) Math.round(change *100) / 100;
其次,mod(%)返回语句的其余部分
14.67%1.0 = 0.67
首先要做的是找到1.0分为14的次数。
dollars = Math.floor(change / 1);
change = change % 1.0;
这会向下舍入到最接近的整数,并且会给出1.0可以进行更改的次数。
在这里使用双人的问题就像Bathsheba所说的那样。 要解决此问题,每次修改更改后都必须进行舍入。
这应该会给你正确的输出。
import java.util.Scanner;
public class RunGivingChange{
public static void main(String[] args) {
double given;
double cost = 85.33;
double change;
int dollars;
int quarters;
int dimes;
int nickels;
int pennies;
Scanner input = new Scanner(System.in);
System.out.println("Your total is " + cost + " dollars.");
System.out.println("How much money are you giving?");
given = input.nextDouble();
if (given >= cost) {
System.out.println("You have given " + given + " dollars.");
change = given - cost;
// round the change
change = (double) Math.round(change *100) / 100;
System.out.println("Your change is " + change + " dollars.");
System.out.println("You will recieve:");
//dollars
System.out.println(Math.floor(change / 1) + " dollars");
change = change % 1;
change = (double) Math.round(change *100) / 100;
//quarters
System.out.println(Math.floor(change / 0.25) + " quarters");
change = change % 0.25;
change = (double) Math.round(change *100) / 100;
//dimes
System.out.println(Math.floor(change / 0.1) + " dimes");
change = change % 0.10;
change = (double) Math.round(change *100) / 100;
//nickels
System.out.println(Math.floor(change / 0.05) + " nickels");
change = change % 0.05;
change = (double) Math.round(change *100) / 100;
//pennies
System.out.println(Math.floor(change / 0.01) + " pennies");
} else if (given < cost) {
System.out.println("Not enough money.");
} else {
System.out.println("Error.");
}
}
}
有更好的方法可以在下次搜索网站时执行此操作。这个主题有多个答案。
How to convert money to change
Compute the different ways to make (money) change from $167.37?