Which of the following number method returns the integer that is closest in value?

The Java Math rint() method returns a value that is closest to the specified value and is equal to the mathematical integer.

That is, if the specified value is 5.8, the closest value that is equal to the mathematical integer is 6.0. And, for value 5.4, the closest value that is equal to mathematical integer is 5.0.

The syntax of the rint() method is:

Math.rint(double value)

Note: The rint() method is a static method. Hence, we can call the method directly using the class name Math.


rint() Parameters

  • arg - argument whose closest value that is equal to mathematical integer is returned

rint() Return Values

  • returns the closest value to arg that is equal to the mathematical integer

Example: Java Math.rint()

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

    // Math.rint()
    // value greater than 5 after decimal
    System.out.println(Math.rint(1.878));  // 2.0

    // value less than 5 after decimal
    System.out.println(Math.rint(1.34));   // 1.0

    // value equal to 5 after decimal
    System.out.println(Math.rint(1.5));    // 2.0

    // value equal to 5 after decimal
    System.out.println(Math.rint(2.5));    // 2.0

  }
}

In the above example, notice the two expressions,

// returns 2.0
Math.rint(1.5)

// returns 2.0
Math.rint(2.5)  

Here, in both cases, the value after the decimal is equal to 5. However,

  • for 1.5 - the method is rounding up
  • for 2.5 - the method is rounding down.

It is because, in the case of .5, the method rounds to nearest even value. Hence, in both cases, the method rounds to 2.0.


  • Math.round()
  • Math.ceil()
  • Math.floor()

Which of the following number method returns the integer that is closest in value?

It's one of the articles from our Java Tutorial for Beginners.


There is a Math class in the package java.lang, which contains 3 methods of rounding of numbers with a floating point to the nearest integer:

1.Math.round()

2.Math.floor()

3.Math.ceil()

Which of the following number method returns the integer that is closest in value?

The names of these methods are self-explanatory.

Which of the following number method returns the integer that is closest in value?

Let’s take a look at the example below and see how these methods work:

Example №1

classTest{

publicstaticvoidmain(String[]args){

floatnum=5.25f;

System.out.println(Math.round(num));

System.out.println(Math.floor(num));

System.out.println(Math.ceil(num));

}

}

If you run this code on your computer, you will see the following in the console:

5
5.0
6.0

Commentary:

1. Math.round () - this method rounds a number to the nearest integer.

And indeed, at first we have 5.25, but the method gives us 5, because 5 is the closest integer number to 5.25. If we rounded 8.75 with this method, we would get 9, because 9 is the closest integer number to 8.75.

Please note that this method returns a value of the int type (i.e. an integer). At first, we have 5.25 and the method gives us 5 instead of 5.0.

2. Math.floor ()- this method rounds a number downward to the nearest integer.

At first, we have 5.25, and the nearest number downward is 5.0. If we rounded the number 8.75 with the help of this method, we would get 8.0, because it 8.0 is the nearest number downward.

You probably now understand why this method is called floor.

Also, please note that this method returns a double-type value. At first, we have the number 5.25, and after rounding we have 5.0: a double type.

3. Math.ceil() - this method rounds a number upward to its nearest integer. At first, we have 5.25, and then this method gives us 6.0. Even if we had 5.01, this method would still return 6.0, because it is the nearest number upward.

That’s why this method is called ceil, which comes from the word "ceiling." Also, please note that this method returns a double-type value.

Below is a table where everything is written schematically:

Which of the following number method returns the integer that is closest in value?

Next, you should learn about the methods Math.random(), Math.max (), and Math.min(). You can read about them in the following articles:

1.Random number generation in Java

2.How to display the lowest and highest value in Java

You can find more articles in our Java Tutorial for Beginners. 


What is RINT method in Java?

rint() is an inbuilt method in Java which is used to round of the floating-point argument to an integer value (in floating-point format). Syntax: Math.rint(double n) Parameters: The rint() function takes a mandatory single argument value to round.

What is the use of RINT function?

The rint() functions return the integral value (represented in a floating-point mode) nearest x using the round to nearest mode and may raise the inexact floating-point exception if the result differs in value from the argument.

What is the return type of Math round?

The Math.round() function returns the value of a number rounded to the nearest integer.

What is the difference between Math round and Math RINT?

Difference is at . Math. round() returns long or int. Math. rint() returns double.