public class Coin {
public void Calculate(double value){
int quarter, dime, nickel, remainder;
value *= 100;
System.out.println(value);
remainder = (int) value;
quarter = (int) value / 25;
if (quarter == 1)
System.out.println("1 quarter");
else
System.out.println(quarter + " quarters");
remainder %= 25;
dime = remainder / 10;
if (dime == 1)
System.out.println("1 dime");
else
System.out.println(dime + " dimes");
remainder %= 10;
nickel = remainder / 5;
if (nickel == 1)
System.out.println("1 nickel");
else
System.out.println(dime + " nickels");
remainder %= 5;
if (remainder == 1)
System.out.println("1 penny");
else
System.out.println(remainder + " pennies");
}
}
输入:2.26 输出: 225.99999999999997 的System.out.println(值); 9个季度 0角钱 0个镍币 0便士
我如何专门使用BigDecimal来解决这个问题,因为问题似乎是值= 225.99999999999997,它应该是226?