Java Output Formatting

  • + 1 comment

    the "f" in printf stands for "Format" if I'm not mistaken.

    printf is an avanced version of print. It allows you to not only print, but format it aswell. This example alone shows some nifty tricks. Some others would be formatting the decimals.

    If you have a decimal of 5.243012 and you only wanted the first 3 places, you can use

    System.out.printf("You Decimal Number is %.3f",myDecimal);
    



    %f means print a float/double and use 3 decimal places.
    the .3 means to print upto the third decimal (Thousandths)



    when you pass variables to your printf command, besure to passthem in the order called.



    EXAMPLE

    System.out.printf("Hello %s, my name is %s. I am %d years old, and you are %d years old",yourName,myName,myAge,yourAge);
    



    As you go on, you want to stop use System.out.print and move onto uses System.out.printf when you're printing data to the screen.

    You won't need to worry about println because adding a \n to your ptintf will add a line whereever the \n is placed.