Java Reference
In-Depth Information
Math.sqrt( 4 ) returns 2.0
Math.sqrt( 10.5 ) returns 3.24
5.10.3 The Rounding Methods
The Math class contains five rounding methods:
/** x is rounded up to its nearest integer. This integer is
* returned as a double value. */
public static double ceil( double x)
/** x is rounded down to its nearest integer. This integer is
* returned as a double value. */
public static double floor( double x)
/** x is rounded to its nearest integer. If x is equally close
* to two integers, the even one is returned as a double. */
public static double rint( double x)
/** Return (int)Math.floor(x + 0.5). */
public static int round( float x)
/** Return (long)Math.floor(x + 0.5). */
public static long round( double x)
For example,
Math.ceil( 2.1 ) returns 3.0
Math.ceil( 2.0 ) returns 2.0
Math.ceil( -2.0 ) returns -2.0
Math.ceil( -2.1 ) returns -2.0
Math.floor( 2.1 ) returns 2.0
Math.floor( 2.0 ) returns 2.0
Math.floor( - 2.0 ) returns -2.0
Math.floor( - 2.1 ) returns -3.0
Math.rint( 2.1 ) returns 2.0
Math.rint( - 2.0 ) returns -2.0
Math.rint( - 2.1 ) returns -2.0
Math.rint( 2.5 ) returns 2.0
Math.rint( 3.5 ) returns 4.0
Math.rint( - 2.5 ) returns -2.0
Math.round( 2.6f ) returns 3 // Returns int
Math.round( 2.0 ) returns 2 // Returns long
Math.round( - 2.0f ) returns -2 // Returns int
Math.round( - 2.6 ) returns -3 // Returns long
Math.round( - 2.4 ) returns -2 // Returns long
5.10.4 The min , max , and abs Methods
The min and max methods are overloaded to return the minimum and maximum numbers of
two numbers ( int , long , float , or double ). For example, max(3.4, 5.0) returns 5.0 ,
and min(3, 2) returns 2 .
The abs method is overloaded to return the absolute value of the number ( int , long ,
float , or double ). For example,
Math.max( 2 , 3 ) returns 3
Math.max( 2.5 , 3 ) returns 3.0
Math.min( 2.5 , 3.6 ) returns 2.5
 
Search WWH ::




Custom Search