Write a Java program to Round off the decimal number using the library method
This Java program reads a double value from the user and then uses the Math.round method to round the value to the nearest integer. Here's a breakdown of the program:
- The import java.util.*; statement imports the java.util package, which contains the Scanner class that is used to read input from the user.
- The public class RoundOff_Decimal statement declares a public class called RoundOff_Decimal, which is the name of the Java file that this program is saved in.
- The public static void main(String[] args) method is the entry point for the program. It takes an array of strings as an argument, but in this program, that argument is not used.
- Inside the main method, a new Scanner object called input is created to read input from the user.
- A double variable num is declared and initialized to 0.
- The program prompts the user to enter a double value using System.out.print and reads it using input.nextDouble().
- The Math.round(num) method is used to round num to the nearest integer.
- The result is then printed to the console using System.out.print.
Source Code
import java.util.*;
public class RoundOff_Decimal
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double num = 0;
System.out.print("Enter the Number : ");
num = input.nextDouble();
System.out.print("Round off the Decimal Number : " + Math.round(num));
}
}
Output
Enter the Number : 88.44
Round off the Decimal Number : 88