(Translated by https://www.hiragana.jp/)
Formatted Output in Java using printf() - GeeksforGeeks
Open In App

Formatted Output in Java using printf()

Last Updated : 12 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Sometimes in programming, it is essential to print the output in a given specified format. Most users are familiar with the printf function in C. Let us discuss how we can Formatting Output with printf() in Java in this article.

Formatting Using Java Printf()

printf() uses format specifiers for formatting. There are certain data types are mentioned below:

  • For Number Formatting
  • Formatting Decimal Numbers
  • For Boolean Formatting
  • For String Formatting
  • For Char Formatting
  • For Date and Time Formatting

i). For Number Formatting

The number itself includes Integer, Long, etc. The formatting Specifier used is %d.

Below is the implementation of the above method:

Java




// Java Program to demonstrate
// Use of printf to
// Formatting Integer
import java.io.*;
  
// Driver Class
class GFG {
      // main function
    public static void main (String[] args) {
        int a=10000;
            
          //System.out.printf("%.d%n",a);
          System.out.printf("%,d%n",a);
    }
}


Output

10,000


ii). For Decimal Number Formatting

Decimal Number Formatting can be done using print() and format specifier %f .

Below is the implementation of the above method:

Java




// Java Programs to demonstrate
// Use of Printf() for decimal
// Number Formatting
import java.io.*;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        // declaring double
        double a = 3.14159265359;
  
        // Printing Double Value with
        // different Formatting
        System.out.printf("%f\n", a);
        System.out.printf("%5.3f\n", a);
        System.out.printf("%5.2f\n", a);
    }
}


Output

3.141593
3.142
 3.14


iii). For Boolean Formatting

Boolean Formatting can be done using printf and ( ‘%b’ or ‘%B’ ) depending upon the result needed.

Below is the implementation of the above method:

Java




// Java Programs to demonstrate
// Use of Printf() for decimal
// Boolean Formatting
import java.io.*;
  
// Driver Function
class GFG {
    // main function
    public static void main(String[] args)
    {
        int a = 10;
        Boolean b = true, c = false;
        Integer d = null;
  
        // Fromatting Done using printf
        System.out.printf("%b\n", a);
        System.out.printf("%B\n", b);
        System.out.printf("%b\n", c);
        System.out.printf("%B\n", d);
    }
}


Output

true
TRUE
false
FALSE


iv). For Char Formatting

Char Formatting is easy to understand as it need printf() and Charracter format specifier used are ‘%c’ and ‘%C’.

Below is the implementation of the above method:

Java




// Java Program to Formatt
//
import java.io.*;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        char c = 'g';
  
        // Formatting Done
        System.out.printf("%c\n", c);
  
        // Converting into Uppercase
        System.out.printf("%C\n", c);
    }
}


Output

g
G


v). For String Formatting

String Formatting requires the knowledge of Strings and format specifier used ‘%s’ and ‘%S’.

Below is the implementation of the above method:

Java




// Java Program to implement
// Printf() for String Formatting
import java.io.*;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
  
        // Formatting from lowercase to
        // Uppercase
        System.out.printf("%s \n", str);
        System.out.printf("%S \n", str);
  
        str = "GFG";
        // Vice-versa not possible
        System.out.printf("%S \n", str);
        System.out.printf("%s \n", str);
    }
}


Output

geeksforgeeks 
GEEKSFORGEEKS 
GFG 
GFG 


vi). For Date and Time Formatting

Formatting of Date and Time is not as easy as the data-type used above. It uses more than simple format specifier knowledge can be observed in the example mentioned below.

Below is the implementation of the above method:

Java




// Java Program to demonstrate use of
// printf() for formatting Date-time
import java.io.*;
import java.util.*;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        Date time = new Date();
  
        System.out.printf("Current Time: %tT\n", time);
  
        // Another Method with all of them Hour
        // minutes and seconds seperated
        System.out.printf("Hours: %tH  Minutes: %tM Seconds: %tS\n"
                          time,time, time);
  
        // Another Method to print the time
        // Followed by am/pm , time in milliseconds
        // nanoseconds and time-zone offset
        System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n",
            time);
    }
}


Output

Current Time: 11:32:36
Hours: 11  Minutes: 32 Seconds: 36
11:32:36 am 198 198000000 +0000 


Note: System.out.format() is equivalent to printf() and can also be used.

Other Methods for Formatting

1. Formatting using DecimalFormat class 

DecimalFormat is used to format decimal numbers.

Below is the implementation of the above method:

Java




// Java program to demonstrate working of DecimalFormat
import java.text.DecimalFormat;
  
// Driver Class
class JavaFormatter2 {
    // main function  
    public static void main(String args[])
    {
        double num = 123.4567;
  
        // prints only numeric part of a floating number
        DecimalFormat ft = new DecimalFormat("####");     
        System.out.println("Without fraction part: num = "
                           + ft.format(num));
  
        // this will print it upto 2 decimal places
        ft = new DecimalFormat("#.##");      
        System.out.println("Formatted to Give precision: num = "
                            + ft.format(num));
  
        // automatically appends zero to the rightmost part
        // of decimal instead of #,we use digit 0
        ft = new DecimalFormat("#.000000");
        System.out.println("appended zeroes to right: num = "
                            + ft.format(num));
  
        // automatically appends zero to the leftmost of
        // decimal number instead of #,we use digit 0
        ft = new DecimalFormat("00000.00");
        System.out.println("formatting Numeric part : num = "
                            + ft.format(num));
  
        // formatting money in dollars
        double income = 23456.789;      
        ft = new DecimalFormat("$###,###.##");
        System.out.println("your Formatted Dream Income : "
                             + ft.format(income));
    }
}


Output

Without fraction part: num = 123
Formatted to Give precision: num = 123.46
appended zeroes to right: num = 123.456700
formatting Numeric part : num = 00123.46
your Formatted Dream Income : $23,456.79


2. Formatting dates and parsing using SimpleDateFormat class

This class is present in java.text package. 

Below is the implementation of the above method:

Java




// Java program to demonstrate working of SimpleDateFormat
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
  
// Driver Class
class Formatter3 {
    // main function
    public static void main(String args[])
        throws ParseException
    {
        // Formatting as per given pattern in the argument
        SimpleDateFormat ft
            = new SimpleDateFormat("dd-MM-yyyy");
        
        String str = ft.format(new Date());
        System.out.println("Formatted Date : " + str);
  
        // parsing a given String
        str = "02/18/1995";
        ft = new SimpleDateFormat("MM/dd/yyyy");
        Date date = ft.parse(str);
  
        // this will print the date as per parsed string
        System.out.println("Parsed Date : " + date);
    }
}


Output

Formatted Date : 24-01-2022
Parsed Date : Sat Feb 18 00:00:00 UTC 1995




Previous Article
Next Article

Similar Reads

PrintWriter printf(Locale, String, Object) method in Java with Examples
The printf(Locale, String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream using the given Locale. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter printf(Locale locale, String format, Object...args) Parameters: This method accepts two mand
2 min read
PrintWriter printf(String, Object) method in Java with Examples
The printf(String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter printf(String format, Object...args) Parameters: This method accepts two mandatory parameter: format which is the format ac
2 min read
PrintStream printf(String, Object) method in Java with Examples
The printf(String, Object) method of PrintStream Class in Java is used to print a formatted string in the stream. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintStream printf(String format, Object...args) Parameters: This method accepts two mandatory parameter: format which is the format ac
2 min read
PrintStream printf(Locale, String, Object) method in Java with Examples
The printf(Locale, String, Object) method of PrintStream Class in Java is used to print a formatted string in the stream using the given Locale. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintStream printf(Locale locale, String format, Object...args) Parameters: This method accepts two mand
2 min read
Console printf(String, Object) method in Java with Examples
The printf(String, Object) method of Console class in Java is used to write a formatted string to the output stream of the console. It uses the specified format string and arguments. It is a convenience method. Syntax: public Console printf(String fmt, Object... args) Parameters: This method accepts two parameters: fmt - It represents the format of
2 min read
Cin-Cout vs Scanf-Printf
Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”. Let us create a dummy input file containing a line with 16 bytes followed by a newline and having 1000000 such lines, making a file of
3 min read
Java IO : Input-output in Java with Examples
Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations. Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide
7 min read
Output of Java Program | Set 1
Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java Code // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); } } Output: 0 0 In Java, a protected member is accessible in all cl
3 min read
Output of Java Program | Set 2
Predict the output of the following Java programs. Question 1: Java Code package main; class Base { public void Print() { System.out.println("Base"); } } class Derived extends Base { public void Print() { System.out.println("Derived"); } } class Main { public static void DoPrint(Base o) { o.Print(); } public static void main(Str
3 min read
Output of Java Program | Set 3
Predict the output of the following Java Programs: Example1: Java Code // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Printing the value inside the object by // above cre
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg