Floating-point numbers are decimal values, which can be rounded to n number of decimal places.
Example:
Input: number = 1.41421356237, round = 3 Output:1.414 Input: number = 0.70710678118, round = 2 Output:0.71
1. Format approach
The decimal number can be rounded by the inbuilt format() method supported by Java. The method has the following syntax :
System.out.format("%.df", number);
Arguments: The first parameter accepts d digits to round off the number to, the second argument accepts a number which is to be rounded.
Implementation:
Java
// Java Program to Round a Number to n Decimal Placesimport java.io.*;class GFG { public static void main(String[] args) { double number = 3.141341435; // rounding number to 2 decimal places System.out.format("%.2f", number); }} |
3.14
2. DecimalFormat Approach
DecimalFormat is a child class of the NumberFormat which is used to perform formatting of decimal numbers in java. We create an object of this class and pass in as arguments the format specified in form of #, with the number of # after decimal point indicating the number of digits we wish to output. By default, the number is rounded off to the ceiling value itself. The object of the DecimalFormat class invokes a method format() which takes as argument the number to be formatted.
Java
// Java Program to Round a Number to n Decimal Placesimport java.text.DecimalFormat;public class Decimal { public static void main(String[] args) { // takes in the input decimal number double number = 145.34567; // create an object of DecimalFormat class DecimalFormat df_obj = new DecimalFormat("#.###"); //.format() is used to print the number in desired //format System.out.println(df_obj.format(number)); }} |
145.346
In case we wish to round off the number to the floor, we invoke the Java in-built class RoundingMode. It has the following values for attributes :
FLOOR - for next nearest floor value CEILING - for next nearest ceiling value
This method can be invoked on the DecimalFormat class supported in-built method setRoundingMode(), which takes as an argument either RoundingMode.FLOOR or CEILING, and accordingly gives us the result.
Java
// Java Program to Round a Number to n Decimal Placesimport java.math.RoundingMode;import java.text.DecimalFormat; public class Decimal { public static void main(String[] args) { // takes in the input decimal number double number = 9.97869896; // create an object of DecimalFormat class DecimalFormat df_obj = new DecimalFormat("#.####"); // round number to the next lowest value df_obj.setRoundingMode(RoundingMode.FLOOR); //.format() is used to print the // number in desired format System.out.println(df_obj.format(number)); }} |
9.9786
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


