NSLog - NSString Formatting


int myInt=6;
float myFloat=78.4342;
NSString *myStr= @"hello world!";

NSLog(@"log: %@ ", myStr);
NSLog(@"log: %f ", myFloat);
NSLog(@"log: %i ", myInt);

//    OTHER FORMAT SPECIFIERS:

//    %d, %i signed int
//    %u unsigned int
//    %f float/double
//    %x, %X hexadecimal int
//    %o octal int
//    %zu size_t
//    %p pointer
//    %e float/double (in scientific notation)
//    %g float/double (as %f or %e, depending on value)
//    %s C string (bytes)
//    %S C string (unichar)
//    %c character
//    %C unichar
//    %lld long long
//    %llu unsigned long long
//    %Lf long double

Another tip:
float floatValue=341.45628;
NSString *stringFromFloat = [[NSString alloc] initWithFormat:@"%0.3f", floatValue, floatValue, floatValue];

NSLog(@"%@",stringFromFloat);
//logs 341.456;


No comments:

Post a Comment