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

Difference between rint() and round() method in Java

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

In this guide, we will discuss the difference between rint() and round() method in Java. Both of these methods belong to the Math class of Java. These methods are used for rounding numbers, however the rules they follow are bit different, especially when the number is exactly halfway between two integers.

Example: For the number 2.5, both these methods produce different output. Integers 2 and 3 are equally close to 2.5.

Math.rint(2.5) returns 2.0, because 2 is closest even integer
Math.round(2.5) returns 3, because 3 is away from zero compared to 2

Math.rint

  • Math.rint(double a) returns the double value nearest to the argument a, and this returned value should be an integer. If two integers are equally close, it rounds to the even integer.

Math.round

  • Math.round(double a) returns the long value nearest to the argument a, and this returned value should be an integer. If two integers are equally close to the argument, it rounds to the number away from zero.

Example Code: rint() vs round()

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

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

System.out.println("\nUsing Math.round:");
for (double value : values) {
long roundResult = Math.round(value);
System.out.println("Math.round(" + value + ") = " + roundResult);
}
}
}

Output:

Using Math.rint:
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
Math.rint(5.5) = 6.0
Math.rint(-5.5) = -6.0

Using Math.round:
Math.round(2.3) = 2
Math.round(2.5) = 3
Math.round(2.7) = 3
Math.round(-2.3) = -2
Math.round(-2.5) = -2
Math.round(-2.7) = -3
Math.round(3.5) = 4
Math.round(4.5) = 5
Math.round(5.5) = 6
Math.round(-5.5) = -5

Explanation

  • Math.rint:
    • For 2.3, it rounds to the nearest integer, which is 2.0.
    • For 2.5, which is exactly halfway between 2 and 3, it rounds to the nearest even integer, which is 2.0.
    • For -2.5, it rounds to the nearest even integer, which is -2.0.
    • For 3.5 and 4.5, it rounds to the nearest even integer, which is 4.0 in both cases.
  • Math.round:
    • For 2.3, it rounds to the nearest integer, which is 2.
    • For 2.5, which is exactly halfway between 2 and 3, it rounds away from zero, which is 3.
    • For -2.5, it rounds away from zero, which is -2.
    • For 3.5 and 4.5, it rounds to the nearest integers, which are 4 and 5, respectively.

Conclusion: These methods produce different output when the given number is exactly halfway between two integers. In such case, rint() returns nearest even integer, while double() returns an integer which is away from zero (means far from zero).

Recommended Posts

  • Java Math.random()
  • Java Math.rint()
  • Java Math.round()
  • 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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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