We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Output Formatting
You are viewing a single comment's thread. Return to all comments →
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
%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
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.