Java Program to Round a Number to n Decimal Places
What is Java Basic Input and Output
In Java, input and output operations are performed through the standard input and output streams. The standard input stream is represented by the System.in object, which is typically connected to the keyboard.
- The standard output stream is represented by the System.out object, which is typically connected to the console.
To know more about Java Program to Round a Number to n Decimal Places read the complete article.
How to Java Program to Round a Number to n Decimal Places?
There are several ways to Java Program to Round a Number to n Decimal Places.
- Steps to program to Round a Number to n Decimal Places.
- Import the java.math.BigDecimal class.
- Define the number to be rounded, for example as a double.
- Create a BigDecimal object from the number.
- Use the setScale method of the BigDecimal object to round the number to N decimal places, specifying the number of decimal places and a rounding mode, such as ROUND_HALF_UP.
- Convert the BigDecimal back to a double if necessary, using the doubleValue method.
Print or use the rounded number as needed.
Note:
The java.lang.Math.round() is used round of the decimal numbers to the nearest value.
Note:
We can use the java.text.DecimalFormat class to To round a number to N decimal places in Java
Let’s look at a Java Program to Round a Number to n Decimal Places is used to perform an operation.
Example: Java Program to Round a Number using format
Run
public class Main { public static void main(String[] args) { double num = 9.87; // Round the number to 2 decimal places String rounded = String.format("%.2f", num); // Convert the rounded string to a double and round it to the nearest whole number double roundedNum = Math.round(Double.parseDouble(rounded)); System.out.println("Rounded number: " + roundedNum); } }
Output
Rounded number: 10.0
Explanation:
This software asks the user to enter a floating-point number, which is subsequently formatted as a string to two decimal places. Using the Math.round() function, the rounded string is then changed into a double and rounded to the closest whole value. The user sees the outcome in full.
Example 2 : Java Program to Round a Number using DecimalFormat
Run
import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double num = 7.78; // Create a DecimalFormat object with 2 decimal places DecimalFormat df = new DecimalFormat("#.00"); // Format the number to 2 decimal places and round it to the nearest whole number double roundedNum = Math.round(Double.parseDouble(df.format(num))); System.out.println("Rounded number: " + roundedNum); } }
Output
Rounded number: 8.0
Explanation:
This software asks the user to enter a floating-point number, which is subsequently formatted with a DecimalFormat object to two decimal places. Using the Math.round() function, the formatted text is then changed to a double and rounded to the nearest whole number. The user sees the outcome in full.
Example 3: Java Program to Round a Number using format
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { double num = 9.9; // Round the number to 2 decimal places String rounded = String.format("%.2f", num); // Convert the rounded string to a double and round it to the nearest whole number double roundedNum = Math.round(Double.parseDouble(rounded)); System.out.println("Rounded number: " + roundedNum); } }
Output
Rounded number: 10.0
Explanation:
Using the Math.round() function, the rounded string is then changed into a double and rounded to the closest whole value. The user sees the outcome in full.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment