user2046417
user2046417

Reputation:

Formatters in java

%b, %c, %d, %f, %s How does this work in java? I have been trying to read Formatter class and formattable interface however, I am unable to understand in regards to the conversions passed as arguments.

For example:

System.out.printf("%f not equals %b", Math.PI, Math.E)

Even though formatter such as %b, %c, %d, %f, %s are limited in the ocjp6 exam, it feels like huge topic to prepare

Upvotes: -1

Views: 506

Answers (2)

Raj
Raj

Reputation: 1965

I think you are having trouble understanding how System.out.printf() works. It is very simple once you get the idea.

You orginal question was regarding below

System.out.printf("%f not equals %b", Math.PI, Math.E)

Here System.out.printf is trying to output a String . %f and %b can be understood as placeholder with special meaning.

Placeholders because they will get replaced with the data that comes after comma. In this case %f is replaced with value of Math.PI and %b is replaced with value of Math.E

Special meaning because each formatter stands for somethings for example as mention above

%b is for Boolean
%f is for Decimal floating-point
%c is for Character
%d is for Decimal integer
%s is for String

Now to write you orginal query in a simple manner

System.out.printf("%f is not equals to %b", 3.14, true);

Here %f (meaning decimal float) is replaced with float value 3.14 and %b is replaced with value "true".

if you switch %f and %b in the above like

System.out.printf("%b is not equal to %f", 3.14, true); //error
 because "true" (boolean)value is not compatible with %f

But this will work

System.out.printf("%b is not equal to %f", 3.14, 3.14); // will work because 3.14 will evaluate to true The above works because of some automatic type conversion by java. But you can look into that later.

Now Regarding your last question what is happening in

System.out.println("%+04.2f",12.6542); ?

I am guessing you meant printf .

Now all formatters and their explanation are present in the link

http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

It might look intimidating . but it is quite easy.

Lets figure out what %+04.2f stands for from the above link 

'+' Requires the output to include a positive sign for all positive numbers. 
    If this flag is not given  only negative values will include sign.

'0' Requires the output to be padded with leading zeros to the minimum field width following any sign. Called zero padding  

4.2 indicates that a floating point number is displayed in a total of 4 character spaces, including 2 digits after the decimal.
    that means 22.5555 will be shown as 22.55(total 4 char and two after space) 
    Read Width and precision in the link given above.

f   floating point  The result is formatted as a decimal number

So Basically what %+04.2f means is show a positive sign for all positive numbers.The number should have 4 characters in total and two after decimal. It should be formatted as floating point number.

More examples

       System.out.printf("  %04.2f",12.6542);   output ==> 12.65
       System.out.printf("  %+04.2f",12.6542);  output ==> +12.65(plus sign here bcoz we gave +)
       System.out.printf("  %+04.2f",-12.6542); output ==> -12.65

       System.out.printf("  %02d",1);   output ==> 1 
       System.out.printf("  %02d",1);   output ==> 01 (bcoz of 02d) 
       System.out.printf("  %03d",1);   output ==> 001 (bcoz of 03d)


       System.out.printf("  %+04.2f",22.2);     output ==> +22.20
       System.out.printf("  %+04.2f",2222.125); output ==> +2222.13 
       (left side of decimal is never truncated . so all chars shows ie total 6 chars even though only 4 asked 

       System.out.printf("  %+04.0f",2222.125); output ==> +2222 (bcoz zero chars requested after decimal point) 

Please go through the below links . It will help you understand the concept more easily

http://www.homeandlearn.co.uk/java/java_formatted_strings.html

https://answers.yahoo.com/question/index?qid=20101017181211AAbtWC0

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

http://alvinalexander.com/programming/printf-format-cheat-sheet

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

%b is for Boolean
%f is for Decimal floating-point
%c is for Character
%d is for Decimal integer
%s is for String

You can check this reference. Also the Oracle docs explains that in detail.

Upvotes: 0

Related Questions