我很感激这方面的帮助。
例如,我的代码是;
class Candy extends DessertItem
{
double weight;
double price;
Candy(String n, double w, double p)
{
super(n);
weight = w;
price = p;
}
public double getCost()
{
double cost = weight*price;
int costinCents = (int)cost*100;
cost = costinCents /100.0;
return cost;
}
public String toString()
{
return name+"\t\t\t\t$" + getCost() + "\n\t" + weight + " lbs @ $" + price;
}
}
输出是:
Peanut Butter Fudge $8.0
2.25 lbs @ $3.99
为什么显示8.0美元而不是8.97美元?
任何帮助将不胜感激!感谢。
答案 0 :(得分:0)
尝试int costinCents = (int) (cost*100)
具体例子: on cost = 3.5 ==> (int)cost * 100 = 300
on cost = 3.5 ==> (int)(费用* 100)= 350