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:
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.
Printing simple integer: x = 100 Formatted with precison: PI = 3.14 Formatted to specific width: n = 5.2000 Formatted to right margin: n = 2324435.2500
System.out.format() is equivalent to printf() and can also be used.
DecimalFormat is used to format decimal numbers.
Without fraction part: num = 123 Formatted to Give precison: num = 123.46 appended zeroes to right: num = 123.456700 formatting Numeric part : num = 00123.46 your Formatted Dream Income : $23,456.79
This class is present in java.text package.
Formatted Date : 09-08-2018 Parsed Date : Sat Feb 18 00:00:00 UTC 1995
This article is contributed by Pankaj Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Output of Java Program | Set 3
- Output of Java Program | Set 11
- Output of Java Program | Set 2
- Output of Java Program | Set 1
- Output of Java Program | Set 4
- Output of Java Program | Set 8
- Output of Java Program | Set 7
- Output of Java Program | Set 6
- Output of Java program | Set 5
- Output of Java Program | Set 9
- Output of Java Programs | Set 12
- Java Tricky Output Questions
- Output of Java Programs | Set 14 (Constructors)
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 54 (Vectors)
Improved By : Mayur_jain