×

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity.

Syntax:

  1. public static int round(float a)
  2. public static long round(double a)

Parameters:

The parameters ‘a’ represents the floating –point number whose rounded value is to be determined.

Return Value:

The round() method returns the rounded value of the argument to its nearest long or int value.

Special cases are as follows:

  • The result is 0, if the argument is NaN.
  • The result is Long.MIN_VALUE, if the argument is negative infinity or any value less or equal to Long.MIN_VALUE.
  • The result is Long.MAX_VALUE, if the argument is positive infinity or any value greater or equal to Long.MAX_VALUE.

Example 1:

public class JavaMathRoundExample1 {
    static  int i=1;
    //returns the rounded value of the argument to its nearest long
    public static void main(String[] args) {
        double a=67.4;
        double b=67.5;
        System.out.println(i++ +". " +Math.round(a));
        System.out.println(i+". "+Math.round(b));
    }
}

Output:

1. 67
2. 68

Example 2:

public class JavaMathRoundExample2 {
    static  int i=1;
    // result is 0, if the argument is NaN
    public static void main(String[] args) {
        double a=Double.NaN;
        System.out.println(i++ +". " +Math.round(a));
    }
}

Output:

1. 0

Example 3:

public class JavaMathRoundExample3 {
    static  int i=1;
    // result is Long.MIN_VALUE, if the argument is negative infinity or any value less or equal to Long.MIN_VALUE.
    public static void main(String[] args) {
        double a=Double.NEGATIVE_INFINITY;
        System.out.println(i++ +". " +Math.round(a));
    }
}

Output:

-9223372036854775808

Example 4:

public class JavaMathRoundExample4 {
    static  int i=1;
    //result is Long.MAX_VALUE, if the argument is positive infinity or any value greater or equal to Long.MAX_VALUE.
    public static void main(String[] args) {
        double a=Double.POSITIVE_INFINITY;
        System.out.println(i++ +". " +Math.round(a));
    }
}

Output:

9223372036854775807

Example 5:

public class JavaMathRoundExample5 {
    static  int i=1;
    //  returns the rounded value of the argument to its nearest int.
    public static void main(String[] args) {
        float a=0.5f;
        System.out.println(i++ +". " +Math.round(a));
    }
}

Output:

1

Related Topics

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.