Java Output Formatting
Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will talk about the varied output formatting in this section.
Sometimes printing the output in a specific format is necessary while using competitive programming. In C, the printf function is widely used. Let's talk about formatting the output in Java.
In Java, formatting the output can be done in one of two ways:
- Making use of the Printf() Method
- Making use of the format () Method
Output Formatting using System.out.printf() method
Because this Method is identical to the printf() function used in C programming, its implementation is quite simple.
OutputFormatting.java
import java .io .*;
public class JavaCallable
{
public static void main ( String args[ ] )
{
// Printing the string using the printf method in Java
String s = " Hello word " ;
System.out.printf( " \n The String value using printf() : %s \n ", s ) ;
// Printing the Integer using the printf method in Java
int i = 45 ;
System.out.printf( " \n The Integer value using printf() : i = %d \n ", i ) ;
// Printing the float using the printf method in Java
float f = 8.456f ;
System.out.printf( " \n The float value using printf() : %f \n ", f ) ;
// formatting the float value to a specific decimal number using %.nf in printf
System.out.printf( " \n Formating the float value to specific decimal : f = %.4f \n ", f ) ;
// formatting the float value to a specific decimal number using %.nf in printf
System.out.printf( " \n Formating the float value to two decimal : f = %.2f \n ", f ) ;
// formatting the float value to a specific right margin number using %n.mf in printf
System.out.printf( " \n Formating the float value right margin : n = %20.4f \n ", f ) ;
}
}
Output
The String value using printf(): Hello word
The Integer value using printf() : i = 45
The float value using printf() : 8.456000
Formating the float value to a specific decimal: f = 8.4560
Formating the float value to two decimal: f = 8.46
Formating the float value right margin : n = 8.4560
printf( ) system.out.format() can also be utilised.
It's vital to remember that, unlike the printf() method, System.out.print() and System.out.println() only accept one parameter.
Output Formatting Using the Decimal Format class
We use the Decimal format class to format the decimal numbers in Java.
import java.text.DecimalFormat ;
// Importing the Decimal format class
public class JavaCallable
{
public static void main( String args[ ] )
{
double num = 987.65432 ;
// Displaying the number initialised in the console
System.out.printf( " \n Double number taken is : %f \n ", num ) ;
// Displaying only the integral part of the double number using decimal format
DecimalFormat obj = new DecimalFormat( " #### " ) ;
System.out.println( " \n Without fraction part the number is : " + obj.format( num ) ) ;
// Displaying only up to the two decimals of the double number using decimal format
obj = new DecimalFormat( " #.## " ) ;
System.out.println( " \n Double number up to two decimal places is = " + obj.format( num ) ) ;
// appending zero to the decimal number on the right side as specified, instead of #, we use the digit 0
obj = new DecimalFormat( " #.000000 " ) ;
System.out.println( " \n Adding zeros to the right part of the double number = " + obj.format( num ) ) ;
// appending zero to the decimal number on the left side as specified, instead of #, we use the digit 0
obj = new DecimalFormat( " 00000.00 " ) ;
System.out.println( " \n Adding zeros to the right part of the double number = "+ obj.format( num ) ) ;
// converting rs into dollars using decimal format class
double money = 550000.789 ;
obj = new DecimalFormat( " $###,###.## " ) ;
System.out.println( " \n Money after converting in to dollars : " + obj.format( money ) ) ;
}
}
Output
Double number taken is : 987.654320
Without fraction part the number is : 988
Double number up to two decimal places is = 987.65
Adding zeros to the right part of the double number = 987.654320
Adding zeros to the right part of the double number = 00987.65
Money after converting in to dollars : $550,000.79