Search code examples
iosnslognsdecimalnumber

ios: NSLog show decimal value


NSNumber *weekNum = [dictionary valueForKey:@"inSeasonFor"];
NSDecimalNumber *newWeekNum = ([weekNum intValue]  *2)/4;
NSLog(@"%", [newWeekNum decimalValue]);

How can I divide weekNum*2 by 4 and keep the decimal value and print it?


Solution

  • You mean you want the fractional part as well, right?

    NSNumber *weekNum = [dictionary valueForKey:@"inSeasonFor"];
    // multiplication by 2 followed by division by 4 is division by 2
    NSLog(@"%f", [weekNum intValue] / 2.0f);
    

    //we can also use intfloat to resolve.