Formatted output in Java

Course Curriculum

Formatted output in Java

Formatted output in Java

Sometimes in Competitive programming, it is essential to print the output in a given specified format. Most users are familiar with printf function in C. Let us see discuss how we can format the output in Java:

Formatting output using System.out.printf()

This is the easiest of all methods as this is similar to printf in C. Note that System.out.print() and System.out.println() take a single argument, but printf() may take multiple arguments.

class Test
public static void main(String args[])
int x = 600;
System.out.printf("Printing simple integer: x = %dn", x);
// this will print it upto 2 decimal places
System.out.printf("Formatted with precison: PI = %.2fn", Math.PI);
float n = 9.2f;
// automatically appends zero to the rightmost part of decimal
System.out.printf("Formatted to specific width: n = %.4fn", n);
n = 9324435.3f;
// here number is formatted from right margin and occupies a
// width of 20 characters
System.out.printf("Formatted to right margin: n = %20.4fn", n);

Printing simple integer: x = 600
Formatted with precison: PI = 3.14
Formatted to specific width: n = 9.2000
Formatted to right margin: n =          9324435.0000

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

Formatting using DecimalFormat class:

  • DecimalFormat is used to format decimal numbers.

// Java program to demonstrate working of DecimalFormat

// Java program to demonstrate working of DecimalFormat
import java.text.DecimalFormat;
class Demo2 {
  public static void main(String args[]) {
    double num = 234.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 precison: 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 = 34456.789;
    ft = new DecimalFormat("$###,###.##");
    System.out.println("your Formatted Dream Income : " + ft.format(income));

Without fraction part: num = 234
Formatted to Give precison: num = 234.46
appended zeroes to right: num = 234.456700
formatting Numeric part : num = 00234.46
your Formatted Dream Income : $34,456.79

Formatting dates and parsing using SimpleDateFormat class:

This class is present in java.text package.

// Java program to demonstrate working of SimpleDateFormat

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Demo3
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 = "05/20/2021";
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);

Formatted Date : 20-05-2021
Parsed Date : Thu May 20 00:00:00 IST 2021

Difference between Scanner and BufferReader Class in Java (Prev Lesson)
(Next Lesson) Fast I/O in Java in Competitive Programming
', { 'anonymize_ip': true });