私はこれにいくつかの助けに感謝したいと思います。メソッドが値を正しく計算して表示していませんか?
たとえば、私のコードは次のとおりです。
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を表示していますか?
助けていただけたら幸いです!ありがとう。
'(int)cost * 100;'とは何と思いますか? – Savior
おっと、私の間違い。小数点以下2桁まで表示するにはどうすればいいですか? – emansour619
http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Savior