printf - Having issue with a very simple java program, not displaying proper result -


here code isn't working:

scanner hello = new scanner (system.in); double = 10; double c;  system.out.print("enter value: "); c = hello.nextdouble(); double f = + c; system.out.printf("the sum of 10 plus user entry : ", a+c); 

no syntax error whatsoever, no error displayed, result : enter value: 100 sum of 10 plus user entry :

so there no result in second line,,, command ( a+c ) in program. if use ' %.2f ' before ( a+c ) command, works fine,, :

system.out.printf("the sum of 10 plus user entry : %.2f", a+c); 

i tried search '%.2f' got know used ascertain following number displayed number 2 decimal places. (kinda round off thing, guess)..

i'm totally rookie @ java. started studying @ college right now. curious know concept , reason behind why program worked '%.2f' typed in it, , not without it, although showed no error. great if can answer it. :-)

java's system.out.printf() method doesn't append information; substitutes it. '%.2f' means: "replace next argument, , convert floating-point number 2 places precise." removing '%.2f' mean a+c have go, , printf() discard it.

since java's system.out.printf() method based on printf() c/c++, might want check out this guide.


Comments