Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

rint()

Example

Round numbers to their nearest integers.

System.out.println(Math.rint(0.5));
System.out.println(Math.rint(1.5));
System.out.println(Math.rint(5));
System.out.println(Math.rint(5.1));
System.out.println(Math.rint(5.1));
System.out.println(Math.rint(5.9));

Definition and Usage

The rint() method in Java rounds a number to the nearest integer. In cases where there are two integers equally close to the number, the even integer is returned.

Note: This method is similar to round(), with key differences:

  • round() returns long or int data types, while rint() returns a double.
  • When the decimal part of the number is exactly 0.5, rint() returns the nearest even integer, whereas round() returns the higher of the two nearest integers.

Syntax

public static double rint(double number)

Parameter Values

Parameter

Description

number

Please provide a number to be rounded.

Technical Details

Returns:

A double value representing the integer nearest to a given number.

Java version:

Any