AppDividend
Latest Code Tutorials

Java Math round() Function Example

0

Java math round() is an inbuilt method that is used to calculate the nearest int(or long) value of a float(or double) variable. The round() method comes handy across a large variety of programs wherever such a conversion is needed. The rounded-off value is calculated by adding 0.5 to the passed value and then taking the floor of the result.

Java Math round()

Java.lang.Math.round() is an inbuilt method that used a round of the decimal numbers to the nearest value. This method is used to return the closest long to the argument, with ties rounding to positive infinity.

Syntax

public static int round(float x)
public static long round(double x)

Parameter(s)

The variable of float or double type whose value is to be rounded off to the closest int or long, respectively.

Return Value

The rounded-off value by adding 0.5 to the passed value and then taking the floor of the result, and after that, typecasting it to int or long depending on whether the passed variable is of float or double type. See the following figure.

 

Java Math round()

Note

  1. If the argument is NaN, then this method returns 0.
  2. If the argument is negative infinity, then this method returns Long.MIN_VALUE.
  3. If the argument is positive infinity, then this method returns Long.MAX_VALUE.

Consider the following examples.

Example1.java: The following example demonstrates how the return values are typecasted by displaying the wrapper classes of the returned variables.

public class Example1 {
  public static void main(String[] args) {
    float a = 4.1f;
    double b = 4.5;

    System.out.println(((Object) (Math.round(a))).getClass().getName());
    System.out.println(((Object) (Math.round(b))).getClass().getName());
  }
}

Output

->javac Example1.java
->java Example1
java.lang.Integer
java.lang.Long

Example2.java: The following example demonstrates how the passed values are rounded off.

public class Example2 {
  public static void main(String[] args) {

    System.out.println("4.6 is rounded off to: " + Math.round(4.6f));
    System.out.println("4.1 is rounded off to: " + Math.round(4.1f));
    System.out.println("3.9 is rounded off to: " + Math.round(3.9f));
    System.out.println("4.50 is rounded off to: " + Math.round(4.50));
    System.out.println("4.51 is rounded off to: " + Math.round(4.51));
    System.out.println("4.49 is rounded off to: " + Math.round(4.49));
  }
}

Output

->javac Example2.java
->java Example2
4.6 is rounded off to: 5
4.1 is rounded off to: 4
3.9 is rounded off to: 4
4.50 is rounded off to: 5
4.51 is rounded off to: 5
4.49 is rounded off to: 4

Example3.java: The following example demonstrates the situation of passing a NaN value.

public class Example3 {
  public static void main(String[] args) {
    System.out.println(Math.round(2.0 % 0));
  }
}

Output

->javac Example3.java
->java Example3
0

Example4.java: The following example demonstrates the situation of passing negative infinity or positive infinity.

public class Example4 {
  public static void main(String[] args) {

    System.out.println(Math.round(-2.0 / 0));
    System.out.println(Math.round(2.0 / 0));
  }
}

Output

->javac Example4.java
->java Example4
-9223372036854775808
9223372036854775807

See also

Java Math min()

Java Math max()

Java Math floor()

Java Math abs()

Java Math copySign()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.