Computer Science, asked by kcgmailcom3742, 1 year ago

Difference between rint function and round function

Answers

Answered by yushaparvej
1
Math.rint() and Math.round() have several differences, but the one which might impact the business logic of a Java application the most is the way they handle rounding of integers which are on a boundary (e.g. 4.5 is on the boundary of 4 and 5). Consider the following code snippet and output:

double val1 = 4.2;
double val2 = 4.5;
System.out.println("Math.rint(" + val1 + ") = " + Math.rint(val1));
System.out.println("Math.round(" + val1 + ") = " + Math.round(val1));
System.out.println("Math.rint(" + val2 + ") = " + Math.rint(val2));
System.out.println("Math.round(" + val2 + ") = " + Math.round(val2));
System.out.println("Math.rint(" + (val2 + 0.001d) + ") = " + Math.rint(val2 + 0.001d));
System.out.println("Math.round(" + (val2 + 0.001d) + ") = " + Math.round(val2 + 0.001d));
Output:

Math.rint(4.2) = 4.0
Math.round(4.2) = 4
Math.rint(4.5) = 4.0
Math.round(4.5) = 5
Math.rint(4.501) = 5.0
Math.round(4.501) = 5
As you can see, Math.rint(4.5) actually rounds down, while Math.round(4.5) rounds up, and this deserves to be pointed out. However, in all other cases, they both exhibit the same rounding rules which we would expect.

Here is a useful Code Ranch article which briefly compares Math.rint() and Math.round():
Answered by xcristianox
3

  • The abs() function is an inbuilt function in PHP which is used to return the absolute (positive) value of a number. ... Return Value: It returns the absolute value of the number passed to it as argument.

Similar questions