Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a format specifier that works with Boolean values?

Tags:

I want to do something like this:

NSLog(@"You got: %x", booleanValue); 

where x is the specifier. But I can't find one! I want to avoid:

if (booleanValue) {     NSLog(@"You got: YES"); } else {     NSLog(@"You got: NO"); } 

Any ideas? The docs didn't have a Boolean specifier. %@ didn't work either.

like image 650
Jay Imerman Avatar asked Jul 19 '11 18:07

Jay Imerman


People also ask

What is the format specifier for boolean?

In C, there is no format specifier for Boolean datatype (bool). We can print its values by using some of the existing format specifiers for printing like %d, %i, %s, etc.

What is boolean in string format?

Format Boolean values The string representation of a Boolean is either "True" for a true value or "False" for a false value. The string representation of a Boolean value is defined by the read-only TrueString and FalseString fields.

How do you print a boolean value?

The println(boolean) method of PrintStream Class in Java is used to print the specified boolean value on the stream and then break the line. This boolean value is taken as a parameter. Parameters: This method accepts a mandatory parameter booleanValue which is the boolean value to be written on the stream.

Is bool a type in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true.


2 Answers

Here are two things that work:

NSLog(@"You got: %@",booleanValue ? @"YES" : @"NO"); 

or you can cast:

NSLog(@"You got: %d", (int)booleanValue); 

Which will output 0 or 1

like image 79
PengOne Avatar answered Oct 04 '22 19:10

PengOne


You can cast it to an int and use %d:

NSLog(@"You got: %d", (int)booleanValue); 

Or use something like this:

NSLog(@"You got: %@", booleanValue ? @"YES" : @"NO"); 
like image 43
Todd Yandell Avatar answered Oct 04 '22 21:10

Todd Yandell