How can you format your numbers with two digits after the decimal point? This is done with the System.out.printf() function, which takes these arguments:

You can see the exact specification for format strings at this URL, but it may be easier to just give you examples.

You can try these examples in a Java program file or in jshell.

Note: You can also use System.out.format(), which is equivalent to System.out.printf().

Formatting Integers

If you want to print an integer that takes up exactly 7 spaces, you could use code like this (try it in a program):

public static void main(String[] args) {
    int x = 1234;
    System.out.printf("The value is %7d%n", x);
}

In this example, the placeholder %7d says to print the variable with a width of seven and the type of the data you are formatting is an integer. (The format string uses d for decimal, even though there are no decimal places. It’s that way because of history.)  The %n at the end means to print a newline character, the same as \n. Unlike System.out.println,  System.out.printf does not automagically provide you with a newline.

When you try this example, you will see that there are three extra spaces at the left to add up to seven spaces total.

What happens if you try to print a number like 12345678 with only 5 spaces width? Try this (I am omitting the "public static void main..." to save space):

int number = 12345678;
System.out.printf("%5d\n", number);

Java prints the whole number. The width is the minimum width you want to take up; if the number takes more than the minimum to display, it uses more.

That number was awfully hard to read. Try printing it using the comma modifier:

System.out.printf("%,5d\n", number);

If you need to print leading zeros on a number, you can specify that by adding a zero before the width. For example:

int hours = 7;
int minutes = 8;
System.out.printf("%02d:%02d%n", hours, minutes); // output is 07:08
int hours2 = 11;
int minutes2 = 25;
System.out.printf("%02d:%02d%n", hours2, minutes2); // output is 11:25

Formatting Doubles

This works very much like integers, except you use f instead of d, (again, because history) and you specify how many places you want to the right of the decimal point. Thus:

double value = 123.4567;
System.out.printf("%7.2f%n", value);

This says to print value with a minimum width of 7 spaces total, two of them to the right of the decimal point (which means four to the left of the decimal point—the decimal point takes up one of those places, and 4 + 2 + 1 = 7.

If you leave off a width and just start with the decimal point, the string will take up exactly as much space as it needs.

Try these in a program or in jshell:

double value = 1234.56789;
System.out.printf("%7.2f%n", value);
System.out.printf("%8.2f%n", value);
System.out.printf("%8.3f%n", value);
System.out.printf("%8.0f%n", value);
System.out.printf("%,8.2f%n", value);
System.out.printf("%,10.2f%n", value);
System.out.printf("%,.2f%n", value);
System.out.printf("%,.3f%n", value);