BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.rint() Method

Last Updated: May 27, 2024 by Chaitanya Singh | Filed Under: java

In this article, we will discuss the rint() method of Math class Java. We will see several programs with explanation to understand this method . Math.rint(double x) method returns the double value that is nearest to the given argument x and equal to a mathematical integer number.

This method rounds to the nearest integer, but if two integers are equally close, it rounds to the even integer. For example, for the number 2.5, this method would return 2, even though 2 and 3 are equally close because 2 is even.

Let’s see an example:

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = 32.5;
    double x2 = 31.5;
    //32 and 33 are equally close to x
    //even number 32 is chosen
    System.out.println(Math.rint(x));

    //31 and 32 are equally close to x
    //even number 32 is chosen
    System.out.println(Math.rint(x2));
  }
}

Output:

32.0
32.0

Another Example:

public class RintExample {
public static void main(String[] args) {
double[] values = { 2.3, 2.5, 2.7, -2.3, -2.5, -2.7, 3.5, 4.5 };

for (double value : values) {
double result = Math.rint(value);
System.out.println("Math.rint(" + value + ") = " + result);
}
}
}

Output:

Math.rint(2.3) = 2.0
Math.rint(2.5) = 2.0
Math.rint(2.7) = 3.0
Math.rint(-2.3) = -2.0
Math.rint(-2.5) = -2.0
Math.rint(-2.7) = -3.0
Math.rint(3.5) = 4.0
Math.rint(4.5) = 4.0

Explanation of the output

  • Math.rint(2.3) returns 2.0 as it is the closest integer to 2.3.
  • Math.rint(2.5) returns 2.0 because, in this case 2 and 3 are equally close but even number is chosen. Here, 2 is even.
  • Math.rint(2.7) returns 3.0 as it is the closest integer.
  • Math.rint(-2.3) returns -2.0 as it is the closest.
  • Math.rint(-2.5) returns -2.0 as it is the closest even integer.
  • Math.rint(-2.7) returns -3.0 as it is the closest.
  • Math.rint(3.5) returns 4.0 because 4 is the closest even integer to 3.5.
  • Math.rint(4.5) returns 4.0 because 4 is the closest even integer to 4.5.

Syntax of Math.rint() method

Math.rint(16.5); //returns 16.0

rint() Description

public static double rint(double x): Returns the closest double value to the argument x, this value is equal to a mathematical integer.

rint() Parameters

  • x: A double value.

rint() Return value

  • Closest floating point value to the argument x, which is equal to an integer.
  • If the argument x is NaN (Not a number), infinity or zero, then it returns the same argument with the same sign.

Example 1

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = 17.5;
    double x2 = -17.5;
    System.out.println(Math.rint(x));
    System.out.println(Math.rint(x2));
  }
}

Output:

Java Math.rint() Example Output1

Example 2

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = 0;
    double x2 = -5.0/0; //-ve infinity
    double x3 = Double.NaN;
    System.out.println(Math.rint(x));
    System.out.println(Math.rint(x2));
    System.out.println(Math.rint(x3));
  }
}

Output:

Java Math.rint() Example Output2

Example 3

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = Double.MAX_VALUE;
    double x2 = Double.MIN_VALUE;
    System.out.println(Math.rint(x));
    System.out.println(Math.rint(x2));
  }
}

Output:

Java Math.rint() Example Output3

Notes:

  • The rint() method is different from Math.round(double a) which rounds half up. I have covered the difference between these two methods here: rint vs round.
  • The behavior of rounding to the nearest even number when exactly halfway is known as “bankers’ rounding.”

Recommended Posts

  • Java Math.random()
  • Java Math.floorDiv()
  • Java Math.floor()
  • Java Math.expm1()
❮ Java Math

Tags: Java Math

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap