定価と割引率から販売価格を求めるプログラムです。
@interface Product : NSObject{
@private
int price;
float disRate;
}
@property int price;
@property float disRate;
@end
@implementation Product
- (int) getSellingPrice{
return price * (1-disRate/100)
}
@synthesize price, disRate;
@end
これが参考書にのっている回答です。
しかし
return price * (1-disRate/100)
この部分は型名を指定しなくてもいいのでしょうか?
return (int) price * (1-(float)disRate/100)
こっちが正解ではないのでしょうか?
おそらく参考書の回答なので間違えているのは私なのですが
理解できないので質問しました。
よろしくお願いします。
>price * (1-disRate/100)
intとfloatではfloatのほうが高精度なので結果はfloatになります。
>(int) getSellingPrice
returnはintにキャストされますので、
この関数の返す値は小数点以下を切り捨てられたintになります。
回答ありがとうございました!
2012/11/03 11:37:24