2012年5月19日 星期六

Basic conversions in Objective-C

 refer to Addison Wesley - Programming.in.ObjectiveC.2.0.2nd (2009)

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
float  f1 = 123.125, f2;
int    i1, i2 = -150;
i1 = f1;   // floating to integer conversion
NSLog (@ ” %f assigned to an int produces %i ” , f1, i1);
f1 = i2;   // integer to floating conversion
NSLog (@ ” %i assigned to a float produces %f ” , i2, f1);
f1 = i2 / 100;   // integer divided by integer
NSLog (@ ” %i divided by 100 produces %f ” , i2, f1);
f2 = i2 / 100.0;   // integer divided by a float
NSLog (@ ” %i divided by 100.0 produces %f ” , i2, f2);
f2 = (float) i2 / 100;   // type cast operator
NSLog (@ ” (float) %i divided by 100 produces %f ” , i2, f2);
[pool drain];
return 0;
}

123.125000 assigned to an int produces 123
-150 assigned to a float produces -150.000000
-150 divided by 100 produces -1.000000
-150 divided by 100.0 produces -1.500000
(float) -150 divided by 100 produces -1.500000

沒有留言: