Java

# Math.ceil and Math.floor in Java

## ceil

“ceil” stands for ceiling (of a room). The number-line in mathematics, for integers, from -10 to +10, is:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10

This is normally written without the + signs; that is:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10

For fraction 5.2, the ceil is 6.0. For the fraction, 2.5, the ceil is 3.0. For the fraction, -5.2, the ceil is -5.0 (and not -6.0). For the number, -2.5, the ceil is -2.0 (and not -3.0).

The ceil of a fraction (improper), is the next integer to the right, on the number line. However, the ceil of an integer is that integer. For example, the ceil of 2 is 2.0; the ceil of 5 is 5.0. Also, “for the nuance”, the ceil of -5 is -5.0, and the ceil of -2 is -2.0.

This means that ceil is applicable only to fractions and not integers. The ceil of an integer is that integer.

## floor

“floor” stands for floor (of a room). For quick (easy) reference, the number line in mathematics, for integers, from -10 to +10, is re-quoted, as:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10

This is normally written without the + signs; that is:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10

For fraction 5.2, the floor is 5.0. For the fraction, 2.5, the floor is 2.0. For the fraction, -5.2, the floor is -6.0 (and not -5.0). For the number, -2.5, the floor is -3.0 (and not -2.0).

The floor of a fraction (improper) is the previous integer on the number line to the left. However, the floor of an integer is that integer. For example, the floor of 2 is 2.0; the floor of 5 is 5.0. Also, “for the nuance”, the floor of -5 is -5.0, and the floor of -2 is -2.0.

This means that floor is applicable only to fractions and not to integers. The floor of an integer is that integer.

## Package

There is a class in Java called Math. This class is in java.lang.* package. When a class is in this package, the package does not have to be imported. The Math class has the methods, ceil() and floor.

### public static double ceil(double a)

This heading is the syntax of the Math ceil method. The number whose ceil is looked for, is the argument. This argument is a double type. This method returns a double type. The method is static, meaning that a Math object does not have to be created to use the method. The class name is used in place of the object name. The method is public, meaning it can be accessed from outside the class code.

The following program, gives the ceil of 5.2:

public class TheClass {
public static void main(String[] args) {
double num = 5.2;
double cl = Math.ceil(num);
System.out.println(cl);
}
}

The output is 6.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.ceil(5.2));
}
}

The following program, gives the ceil of 2.5:

public class TheClass {
public static void main(String[] args) {
double num = 2.5;
double cl = Math.ceil(num);
System.out.println(cl);
}
}

The output is 3.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.ceil(2.5));
}
}

The following program, gives the ceil of -5.2:

public class TheClass {
public static void main(String[] args) {
double num = -5.2;
double cl = Math.ceil(num);
System.out.println(cl);
}
}

The output is -5.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.ceil(-5.2));
}
}

The following program, gives the ceil of -2.5:

public class TheClass {
public static void main(String[] args) {
double num = -2.5;
double cl = Math.ceil(num);
System.out.println(cl);
}
}

The output is -2.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.ceil(-2.5));
}
}

Remember: The ceil of a fraction (improper), is the next integer to the right, on the number line. However, the ceil of an integer is that integer.

### public static double floor(double a)

This heading is the syntax of the Math floor method. The number whose floor is looked for, is the argument. This argument is a double type. This method returns a double type. The method is static, meaning that a Math object does not have to be created to use the method. The class name is used in place of the object name. The method is public, meaning that it can be accessed from outside the class code.

The following program, gives the floor of 5.2:

public class TheClass {
public static void main(String[] args) {
double num = 5.2;
double fr = Math.floor(num);
System.out.println(fr);
}
}

The output is 5.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.floor(5.2));
}
}

The following program, gives the floor of 2.5:

public class TheClass {
public static void main(String[] args) {
double num = 2.5;
double fr = Math.floor(num);
System.out.println(fr);
}
}

The output is 2.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.floor(2.5));
}
}

The following program, gives the floor of -5.2:

public class TheClass {
public static void main(String[] args) {
double num = -5.2;
double fr = Math.floor(num);
System.out.println(fr);
}
}

The output is -6.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.floor(-5.2));
}
}

The following program, gives the floor of -2.5:

public class TheClass {
public static void main(String[] args) {
double num = -2.5;
double fr = Math.floor(num);
System.out.println(fr);
}
}

The output is -3.0. For the same output, the code could be reduced to:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.floor(-2.5));
}
}

Remember: The floor of a fraction (improper) is the previous integer on the number line to the left. However, the floor of an integer is that integer.

## Conclusion

The ceil of a fraction (improper), is the next integer to the right, on the number line. However, the ceil of an integer is that integer. The syntax for the Math class method to obtain a ceil, in Java, is:

public static double ceil(double a)

A statement example of its use, is:

System.out.println(Math.ceil(2.5));

giving an output of 3.0.

The floor of a fraction (improper) is the previous integer on the number line to the left. However, the floor of an integer is that integer. The syntax for the Math class method to obtain a floor, in Java, is:

public static double floor(double a)

A statement example of its use, is:

System.out.println(Math.floor(2.5));

giving an output of 2.0. 