0

I was trying to cast a double value to int and I got a different value.

I have this code:

double double100times = 240;
int a=[[NSNumber numberWithDouble:double100times] intValue];

and the value I get from this is a=239.

If I use int a= (int)double100times instead I get the same value.

Why is this?

UPDATE: If I use double double100times = 240; works, but I'm using:

 double double100times = (2.3*100)+10;
4

1 に答える 1

0

このコードを試しました

double double100times = 240;
    int a=[[NSNumber numberWithDouble:double100times] intValue];
    NSLog(@"Integer : %d",a);

それは私に結果をもたらします

整数:240

よろしいですか、ログをもう一度確認してください。

編集 :-

回避策。

double double100times = ((2.3*100)+10);
    NSLog(@"Value : %f",double100times);
    int a=[[NSNumber numberWithDouble:double100times] intValue];
    int b = [[NSString stringWithFormat:@"%f",double100times] intValue];
    NSLog(@"Integer : %d",a);
    NSLog(@"Integer : %d",b);
于 2013-02-11T12:35:09.083 に答える