Javaexercise.com

How to print a float with 2 decimal places in Java

In Java, float type represents a floating-point value that contains several digits after the decimal point. Although all the digits are important for mathematical computation but generally only two or three digits after the decimal point are significant. 

In this article, we will learn to get a float value with 2 decimal places only. We will use several methods such as setMaximumFractionDigits(), format(), printf(), etc in our examples. Let's see the examples.

Example 1: Use printf() method to print the float value with 2 decimal place

Java provides printf() method to print formatted output to the console. It is similar to printf() method of C language, if you are familiar with it then you can easily relate that. This method takes the first argument as a formatted string that specifies the output format.

/* 
 * Example to print float value with 2 decimal points in Java 
 */
public class JExercise {
	public static void main(String[] args) {
		float a = 1111;
		float b = 7;
		float result = a/b;
		// Display result
		System.out.println(result);
		// Prints only 2 decimal points
		System.out.printf("%.2f", result);
	}
}

Output:

158.71428
158.71

 

Example 2: Use format() method to print the float value with 2 decimal place

In this example, we used format() method of DecimalFormat class to get float value with 2 decimal points. You can see that we used setMaximumFractionDigits() method to specify the fraction digit limit. 

import java.text.DecimalFormat;

/* 
 * Example to print float value with 2 decimal points in Java 
 */
public class JExercise {
	public static void main(String[] args) {
		float a = 1111;
		float b = 7;
		float result = a/b;
		// Display result
		System.out.println(result);
		DecimalFormat dfrmt = new DecimalFormat();
		dfrmt.setMaximumFractionDigits(2);
		// Prints only 2 decimal points
		System.out.println(dfrmt.format(result));
	}
}

Output:

158.71428
158.71

 

Example 3: Use format() method of String class to print the float value with 2 decimal place

Java String class provides a format() method to format a value and returns a string which further can be converted into float by using the valueOf() method of Float class.

/* 
 * Example to print float value with 2 decimal points in Java 
 */
public class JExercise {
	public static void main(String[] args) {
		float a = 1111;
		float b = 7;
		float result = a/b;
		// Display result
		System.out.println(result);
		String str = String.format("%.02f", result);
		result = Float.valueOf(str);
		// Prints only 2 decimal points
		System.out.print(result);
	}
}

Output:

158.71428
158.71

 

Example 3: Use format("#.##") method to print the float value with 2 decimal place

The DecimalFormat class's constructor accepts a pattern that specifies the format of the output. You can see the by using this trick, we don't need to use the setMaximumFractionDigits() method. It works fine and produces the desired result.

import java.text.DecimalFormat;

/* 
 * Example to print float value with 2 decimal points in Java 
 */
public class JExercise {
	public static void main(String[] args) {
		float a = 1111;
		float b = 7;
		float result = a/b;
		// Display result
		System.out.println(result);
		DecimalFormat dfrmt = new DecimalFormat("#.##");
		// Prints only 2 decimal points
		System.out.println(dfrmt.format(result));
	}
}

Output:

158.71428
158.71

 


Conclusion

Well, in this topic, we have learned to get a float value with 2 decimal places. We used several methods and classes such as String class, DecimalFormat class, etc that provide format method to format the input into the desired output.

If we missed something, you can suggest us at - info.javaexercise@gmail.com