Java Reference
In-Depth Information
Mathematical Operations
The Math object also has several methods to carry out a variety of useful mathematical
operations.
The Math.abs() method returns the absolute value of a number. So if the number is pos-
itive, it will remain the same and if it is negative, it will become positive:
> Math.abs(3);
<< 3
> Math.abs(-4.6);
<< 4.6
The Math.ceil() method will round a number up to the next integer, or remain the
same if it is already an integer:
> Math.ceil(4.2);
<< 5
> Math.ceil(8);
<< 8
> Math.ceil(-4.2);
<< -4
The Math.floor() method will round a number down to the next integer, or remain the
same if it is already an integer:
> Math.floor(4.2);
<< 4
> Math.floor(8);
<< 8
> Math.floor(-4.2);
<< -5
Search WWH ::




Custom Search