×

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

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.