Search code examples
iosobjective-cios6

Objective C Converting float to integer produces strange results


I have float which has value 30 which I get from core data.

When I print the value using NSLog I get 30.

NSLog(@"The value of float temp is %@", [self.detailItem valueForKey:@"credit"]);

When I print value by casting float to int I get strange numbers and different result each time log is printed:

NSLog(@"The value of float temp is %d", (int)[self.detailItem valueForKey:@"credit"]);

137978864
122273424

Solution

  • try this

    NSLog(@"The value of float temp is %d", [[self.detailItem valueForKey:@"credit"] intValue]);