![]() |
VOOZH | about |
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.
printf() uses format specifiers for formatting. There are certain data types are mentioned below:
The number itself includes Integer, Long, etc. The formatting Specifier used is %d.
10,000
Decimal Number Formatting can be done using print() and format specifier %f .
3.141593 3.142 3.14
Boolean Formatting can be done using printf and ( '%b' or '%B' ) depending upon the result needed.
true TRUE false FALSE
Char Formatting is easy to understand as it need printf() and Charracter format specifier used are '%c' and '%C'.
g G
String Formatting requires the knowledge of Strings and format specifier used '%s' and '%S'.
geeksforgeeks GEEKSFORGEEKS GFG GFG
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.
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.
DecimalFormat is used to format decimal numbers.
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
This class is present in java.text package.
Formatted Date : 24-01-2022 Parsed Date : Sat Feb 18 00:00:00 UTC 1995