Formatted Output in Java
We, sometimes need the output of a program to be printed in a specified format. This is possible in the C programming language by utilising the printf() function. This section will go over the various output formatting options.
Let us now look at how we can format the output in Java.
There are three mainly three approaches to format output in Java. They are given below.
- Using the System.out.printf
- Making use of the DecimalFormat class
- Using the SimpleDateFormat class to format dates
Approach: Using the System.out.printf
This method is the simplest of all because it is equivalent to printf in C. It's important to understand that System.out.print() and System.out.println() only accept one parameter, whereas printf() can accept several arguments.
Implementation:
FileName: Formatter1.java
import java.io.*; import java.util.*; public class Formatter1 { public static void main(String args[]) { int a = 100; System.out.printf("Print the integer: a = %d ", a); // This will print it to the nearest two decimal places. System.out.printf("Formatted it with the precision: PI = %.2f ",Math.PI); float b = 5.2f; // automatically adds a zero to the rightmost component of the decimal System.out.printf("Format to the specific width: b = %.4f ", b); b = 2324435.3f; // Here, the number is formatted from the right margin and // has a width of 20 characters. System.out.printf("Format to the right margin: b = %20.4f ", b); } }
Output:
Print the integer: a = 100 Formatted it with the precision: PI = 3.14 Format to the specific width: b = 5.2000 Format to the right margin: b = 2324435.2500
Approach: Making use of the DecimalFormat class
The DecimalFormat class is used to format decimal integers.
Implementation:
FileName: Formatter2.java
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class Formatter2 { public static void main(String args[]) { double n = 123.4567; // only prints the numeric portion of a floating number DecimalFormat ft = new DecimalFormat("####"); System.out.println("Without the fraction part: n = "+ ft.format(n)); // This will print it to two decimal places. ft = new DecimalFormat("#.##"); System.out.println("Format to Give the precision: n = "+ ft.format(n)); // Instead of #, we use digit 0 to automatically attach zero to the rightmost part of a decimal. ft = new DecimalFormat("#.000000"); System.out.println("Appended zeroes to right of the number: n = " + ft.format(n)); // Instead of #, we use digit 0 to automatically append zero to the leftmost decimal integer. ft = new DecimalFormat("00000.00"); System.out.println("format the Numeric part : n = "+ ft.format(n)); // formatting money in dollars double b = 23456.789; ft = new DecimalFormat("$###,###.##"); System.out.println("Formatted Income : "+ ft.format(b)); } }
Output:
Without the fraction part: n = 123 Format to Give the precision: n = 123.46 Appended zeroes to right of the number: n = 123.456700 Format the Numeric part : n = 00123.46 Formatted Income : $23,456.79
Approach: Using the SimpleDateFormat class to format dates
The java.text class.In Java, the SimpleDateFormat class includes methods for formatting and parsing dates and times. SimpleDateFormat is a concrete class that inherits from java.text and is used to format and parse dates.DateFormat is an example of a class.
Implementation:
FileName: Formatter3.java
import java.io.*; import java.util.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Formatter3 { public static void main(String args[])throws ParseException { // Formatting based on the pattern specified in the parameter SimpleDateFormat ft = new SimpleDateFormat("dd-MM-yyyy"); String s = ft.format(new Date()); System.out.println("Formatted Date is given by : " + s); // parsing a given text String s = "01/01/2001"; ft = new SimpleDateFormat("MM/dd/yyyy"); Date d = ft.parse(s); // This will output the date according to the parsed text. System.out.println("Parsed Date is given by : " + d); } }
Output:
Formatted Date is given by : 01-09-2023 Parsed Date is given by : Mon Jan 01 00:00:00 GMT 2001