LoginSignup
2
2

More than 5 years have passed since last update.

NSNumberを文字列化する

Last updated at Posted at 2015-10-14

stringValueを使うと、

ちょっとちっちゃくなったり
NSNumber *num = @66.6;
NSLog(@"%f", num.doubleValue);//66.600000
NSLog(@"%@", num.stringValue);//66.59999999999999
ちょっとおっきくなったり
NSNumber *num = @99.9;
NSLog(@"%f", num.doubleValue);//99.900000
NSLog(@"%@", num.stringValue);//99.90000000000001

しちゃうことがあるので、面倒でも、

stringWithFormat
NSNumber *num = @66.6;
NSLog(@"%@", [NSString stringWithFormat:@"%f",num.doubleValue]);//66.600000

とか、

NSNumberFormatter
NSNumber *num = @99.9;
NSNumberFormatter *nf = NSNumberFormatter.new;
nf.numberStyle = NSNumberFormatterDecimalStyle;
NSLog(@"%@", [nf stringFromNumber:num]);//99.9

を使った方が良さそうです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2