public static double rint ( double x ) { double y = floor ( x ) ; double d = x - y ; if ( d > 0.5 ) { if ( y == - 1.0 ) { return - 0.0 ; // Preserve sign of operand } return y + 1.0 ; } if ( d < 0.5 ) { return y ; } /* half way, round to even */ long z = ( long ) y ; return ( z & 1 ) == 0 ? y : y + 1.0 ; }
Get the whole number that is the nearest to x or the even one if x is exactly half way between two integers .