トップページ » [1].Objective-C文法 » データ型まとめ

データ型まとめ

データ型のまとめです。

int型
整数をあつかうときの型 。小数点を使うとエラーが出る。

double float型
少数をあつかうときの型。


■文字列に代入する


int 整数型の場合
int score=21;
str=[NSString stringWithFormat:@"%d",score];

double少数型の場合
double score=21.394;
str=[NSString stringWithFormat:@"%f",score];

NSString のインスタンス
整数 %d
少数 %f
文字 %s


■四捨五入 小数点以下を四捨五入して表示したい場合

%.1f
少数点一桁だけ表示して 以下は四捨五入して表示する

%.2f
少数点2桁だけ表示して 以下は四捨五入して表示する

実際に書くと

score=21.394;
str=[NSString stringWithFormat:@"%.1f",score];
とすれば 文字列 str には 21.4 が入力される

score=21.394;
str=[NSString stringWithFormat:@"%.2f",score];
とすれば 文字列 str には 21.39 が入力される


参考サイト
http://www.atmarkit.co.jp/fcoding/articles/objc/04/objc04a.html






by   at 18:08