Formatting With Printf

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will be using the printf() method in Java. The printf() method is used to format and print a string to the console. The general syntax of the method is:


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/format("`Format`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117408{{"`Formatting With Printf`"}} java/format -.-> lab-117408{{"`Formatting With Printf`"}} java/class_methods -.-> lab-117408{{"`Formatting With Printf`"}} java/date -.-> lab-117408{{"`Formatting With Printf`"}} java/modifiers -.-> lab-117408{{"`Formatting With Printf`"}} java/oop -.-> lab-117408{{"`Formatting With Printf`"}} java/identifier -.-> lab-117408{{"`Formatting With Printf`"}} java/arrays -.-> lab-117408{{"`Formatting With Printf`"}} java/data_types -.-> lab-117408{{"`Formatting With Printf`"}} java/operators -.-> lab-117408{{"`Formatting With Printf`"}} java/strings -.-> lab-117408{{"`Formatting With Printf`"}} java/variables -.-> lab-117408{{"`Formatting With Printf`"}} java/string_methods -.-> lab-117408{{"`Formatting With Printf`"}} java/system_methods -.-> lab-117408{{"`Formatting With Printf`"}} end

Setting up the project directory

Open the terminal and create a project directory using the command:

mkdir project

Create a new file inside the project directory using the command:

touch project_formatting_printf.java

Formatting Integers

To format integers using printf() we use the %d format specifier. Below is an example:

public static void main(String[] args) {
  int num = 10;
  System.out.printf("The number is: %d%n", num);
}

In the above code, %d is the format specifier for integers and %n is the newline character.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

The number is: 10

Formatting Decimals

To format decimals, use the %f format specifier. For example:

public static void main(String[] args) {
  double num = 3.14159265359;
  System.out.printf("The number is: %.2f%n", num);
}

In the above code, %.2f is the format specifier for decimals with 2 decimal places.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

The number is: 3.14

Formatting Dates

To format dates, we use t as the conversion character and give a format specifier. For example:

public static void main(String[] args) {
  Date date = new Date();
  System.out.printf("Today is %tA, %<tB %<te, %<tY.%n", date);
}

In the above code, %tA is the format specifier for the full name of the day and %<tB %<te, %<tY is the format specifier for the month, day and year. %n is the newline character.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

Today is Friday, October 15, 2021.

Using Flags

Flags are used to add special features to the output. For example:

public static void main(String[] args) {
  double num = 12345.6789;
  System.out.printf("The number is: %,.2f%n", num);
}

In the above code, , is the thousands separator and %.2f is the format specifier for decimals with 2 decimal places.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

The number is: 12,345.68

Using Precision

Precision is used to limit the number of decimal places. For example:

public static void main(String[] args) {
  double num = 1234.56789;
  System.out.printf("The number is: %.2f%n", num);
}

In the above code, %.2f is the format specifier for decimals with 2 decimal places.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

The number is: 1234.57

Using Uppercase

To print the output in uppercase, we use the uppercase of the conversion character. For example:

public static void main(String[] args) {
  String name = "John Doe";
  System.out.printf("Name: %S%n", name);
}

In the above code, %S is the format specifier for uppercase strings.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

Name: JOHN DOE

Using Padding

We can use padding to add spaces or zeroes to the output. For example:

public static void main(String[] args) {
  int num = 123;
  System.out.printf("The number is: %010d%n", num);
}

In the above code, %010d is the format specifier for integers with 10 digits and the padding character is 0.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

The number is: 0000000123

Combining Format Specifiers

We can combine multiple format specifiers to format the output as we want. For example:

public static void main(String[] args) {
  double num = 1234.56789;
  System.out.printf("The number is: %,010.2f%n", num);
}

In the above code, %010.2f is the format specifier for decimals with 2 decimal places, 10 digits, and the padding character is 0. , is the thousands separator.

Compile and run the code using the following commands:

javac project_formatting_printf.java
java project_formatting_printf

You will see the output:

The number is: 01,234.57

Summary

Congratulations! You have completed the Formatting With Printf lab. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like