scjp - Formatters in java -


%b, %c, %d, %f, %s how work in java? have been trying read formatter class , formattable interface however, unable understand in regards conversions passed arguments.

for example:

system.out.printf("%f not equals %b", math.pi, math.e)

even though formatter such %b, %c, %d, %f, %s limited in ocjp6 exam, feels huge topic prepare

i think having trouble understanding how system.out.printf() works. simple once idea.

you orginal question regarding below

system.out.printf("%f not equals %b", math.pi, math.e) 

here system.out.printf trying output string . %f , %b can understood placeholder special meaning.

placeholders because replaced data comes after comma. in case %f replaced value of math.pi , %b replaced value of math.e

special meaning because each formatter stands somethings example mention above

%b boolean %f decimal floating-point %c character %d decimal integer %s string 

now write orginal query in simple manner

system.out.printf("%f not equals %b", 3.14, true); 

here %f (meaning decimal float) replaced float value 3.14 , %b replaced value "true".

if switch %f , %b in above

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

but work

system.out.printf("%b not equal %f", 3.14, 3.14); // work because 3.14 evaluate true above works because of automatic type conversion java. can later.

now regarding last question happening in

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

i guessing meant printf .

now formatters , explanation present in link

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

it might intimidating . quite easy.

lets figure out %+04.2f stands above link   '+' requires output include positive sign positive numbers.      if flag not given  negative values include sign.  '0' requires output padded leading zeros minimum field width following sign. called 0 padding    4.2 indicates floating point number displayed in total of 4 character spaces, including 2 digits after decimal.     means 22.5555 shown 22.55(total 4 char , 2 after space)      read width , precision in link given above.  f   floating point  result formatted decimal number 

so %+04.2f means show positive sign positive numbers.the number should have 4 characters in total , 2 after decimal. should formatted 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 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 never truncated . chars shows ie total 6 chars though 4 asked          system.out.printf("  %+04.0f",2222.125); output ==> +2222 (bcoz 0 chars requested after decimal point)  

please go through below links . understand concept more

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


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -