Page 145 - CA_Blue( J )_Class9
P. 145
7.2.8 Math.round()
It returns the value after rounding it up to the nearest integer number making the pivot point as 0.5. The return
type depends on the types of arguments passed.
If it is an integer value, then it will return the same value. For example:
int i = Math.round(45); // Output: 45
If it is a real positive value, then the value after the decimal is considered according to the following:
• If the decimal value is less than 0.5, then the output will be integer value only. For example:
int i = Math.round(45.42); You will get the following output: : 45
int j = Math.round(34.09); You will get the following output: : 34
• If the decimal value is more than or equal to 0.5, then the output will be the smallest integer value greater
than the number. For example:
int i = Math.round(45.52); You will get the following output: : 46
int j = Math.round(45.99); You will get the following output: : 46
If it is a real negative value, then the value after the decimal is considered according to the following:
• If the decimal value is -0.5 or greater, then the output will be integer value only. The sign will be negative. For
example:
int i = Math.round(-35.5); You will get the following output: : -35
int j = Math.round(-46.01); You will get the following output: : -46
• If the decimal value less than -0.5, then the output will be the largest integer value smaller than the number.
For example:
int i = Math.round(-15.62); You will get the following output: : -16
int j = Math.round(-10.90); You will get the following output: : -11
7.2.9 Math.ceil()
It returns the smallest integer value greater than the number that is provided in the argument. The output data
type will be double.
Examples of Math.ceil() Output
System.out.println(Math.ceil(124.5)); 125.0
int d = (int)Math.ceil(25.8); 26
double d = Math.ceil(-1.0/2.0); -0.0
System.out.println(Math.ceil(-16.05)); -16.0
7.2.10 Math.floor()
It returns the largest integer value smaller than the number that is provided in the argument. The output data
types will be double.
Examples of Math.floor() Output
System.out.println(Math.floor(124.5)); 124.0
int d = (int)Math.floor(25.8); 25
double d = Math.floor(-1.0/2.0); -1.0
System.out.println(Math.floor(-16.05)); -17.0
Mathematical Library Methods 143

