Computer Applications
Predict the output of the following:
System.out.println(Math.rint(-99.4));
Answer
-99.0
Working
Math.rint method rounds off its argument to the nearest mathematical integer and returns its value as a double type. The nearest integer to -99.4 is -99.0 so that is the output. Math.rint method behaves in a particular way at the mid-point i.e. when the decimal part of the argument is 0.5. In such cases, the result is the integer value that is even. Let's understand this with an example. Math.rint(1.5) and Math.rint(2.5) will both return 2.0. In the case of 1.5, both 1.0 and 2.0 are equally close to 1.5. Math.rint choses the integer that is even so 2.0 is returned. In the case of 2.5, both 2.0 and 3.0 are equally close to 2.5. Math.rint again choses the integer that is even so 2.0 is returned.
Related Questions
Predict the output of the following:
System.out.println(Math.sqrt(10.24));
Predict the output of the following:
System.out.println(Math.cbrt(42.875));
Predict the output of the following:
System.out.println(Math.min(-25.5, -12.5));
Predict the output of the following:
System.out.println(Math.ceil(-0.95));