Formatted output in Java

 Competitive programming often requires output to be formatted in a specific manner. While many programmers are familiar with C's printf function, Java offers several ways to format output. In this article, we will explore three of these methods.

  1. Using System.out.printf()

The System.out.printf() method is the most straightforward method of formatting output in Java. Unlike System.out.print() and System.out.println(), which take a single argument, printf() can take multiple arguments. Here is an example of how to use printf():

java
public class JavaFormatter1 { public static void main(String args[]) { int x = 100; System.out.printf("Printing simple integer: x = %d\n", x); // this will print it up to 2 decimal places System.out.printf("Formatted with precision: PI = %.2f\n", Math.PI); float n = 5.2f; // automatically appends zero to the rightmost part // of decimal System.out.printf("Formatted to specific width: n = %.4f\n", n); n = 2324435.3f; // here number is formatted from right margin and // occupies a width of 20 characters System.out.printf("Formatted to right margin: n = %20.4f\n", n); } }

In the example above, we have used printf() to format integers, floating-point numbers, and strings. Note that System.out.format() is equivalent to printf() and can also be used.

  1. Using DecimalFormat class

The DecimalFormat class is used to format decimal numbers. Here's an example of how to use it:

java
public class JavaFormatter2 { 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 up to 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)); } }

In the example above, we have used DecimalFormat to format a number in various ways, including rounding to two decimal places, appending zeroes to the rightmost part of the decimal, formatting the numeric part, and formatting money in dollars.

  1. Using SimpleDateFormat class (for formatting Dates)

The SimpleDateFormat class is used to format and parse dates. Here's an example of how to use it:

java
public class JavaFormatter3 { 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/199


The article discusses different ways to format the output in Java, which is often essential in competitive programming. It begins by introducing the printf() function, which is similar to printf in C, and demonstrates how it can be used to format the output. The printf() function can take multiple arguments and is useful for printing output with different formats.

The article then moves on to discuss the DecimalFormat class, which is used to format decimal numbers. It provides examples of different formats that can be used to format the output to a specific precision, width, or with appended zeroes. Additionally, the article demonstrates how the DecimalFormat class can be used to format monetary values.

Finally, the article discusses the SimpleDateFormat class, which is used to format and parse dates. The SimpleDateFormat class can be used to format the date according to a specified pattern, and it can also be used to parse a given string into a Date object. The article provides an example of how the SimpleDateFormat class can be used to format a date in a specific format and parse a given string into a Date object.

Overall, the article provides a comprehensive overview of different ways to format the output in Java. It is written in a concise and clear manner, making it easy for beginners to understand and follow. The examples provided in the article are also helpful in demonstrating the different formatting techniques.

Previous Post Next Post