-1

%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

  • what particular part of `Formatter` code you don't understand? – Alex Salauyou May 12 '15 at 09:03
  • @Sasha: %b, %c, %d, %f, %s –  May 12 '15 at 09:04
  • how they're used or how they're processed behind the scenes? – Alex Salauyou May 12 '15 at 09:37
  • @Sasha How are they used? for example `System.out.println("%+04.2f",12.6542);` –  May 12 '15 at 09:46
  • `System.out.format("%+04.2f",12.6542);` will output `+12.65`. `%` means placeholder, `+` means "print sign, + or -", `0` means "print leading zeroes to fill minimum width", `4` means "width of output 4 chars minimum", `.2` means "2 chars after decimal point", `f` means "treat argument as float-point number" – Alex Salauyou May 12 '15 at 09:53
  • `System.out.format("%+04.2f", 0.1);` will result `+0.10` guided by the same logic. – Alex Salauyou May 12 '15 at 09:54
  • @Sashs uff thats big. need to practice it. printf functions are more familiar to people who work on c language I guess. Any way Thank you much Sasha :) –  May 12 '15 at 10:10

2 Answers2

2

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

Raj
  • 1,945
  • 20
  • 40
  • Reason for displaying integers as true by format specifier %b is explained here http://stackoverflow.com/questions/8629995/formatting-using-printf-and-format – Ajay Sharma Jul 22 '15 at 11:05
0
%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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • what is happening in this code `System.out.printf("%f not equals %b", Math.PI, Math.E)` –  May 12 '15 at 09:12
  • @KiritiK:- As answered above %f is for decimal floating point. So Math.PI is evaluated as floating point. Whereas Math.E is evaluated as boolean in this case – Rahul Tripathi May 12 '15 at 09:14
  • so %f is applicable to Math.PI and %b is applicable to Math.E right? This is what I am looking for. –  May 12 '15 at 09:18
  • So it is based on the position of argument where %f is applicable to first exp Math.PI and so on ? –  May 12 '15 at 09:19
  • Also similary what is happening in `System.out.println("%+04.2f",12.6542);` –  May 12 '15 at 09:30