java ceil

java ceil

In Java, the Math.ceil() method is used to round a decimal number up to the nearest integer. This method takes a double value as an argument and returns the smallest integer that is greater than or equal to the input value.

Here's an example of how you can use the Math.ceil() method:

ref‮t re‬o:lautturi.com
double x = 3.7;
double y = Math.ceil(x);

System.out.println(y); // Outputs: 4.0

In this example, the value of x is 3.7, which is between 3 and 4. The Math.ceil() method will round this value up to 4.0, which is the nearest integer that is greater than 3.7.

You can also use the Math.ceil() method to round up a negative decimal number. For example:

double x = -3.7;
double y = Math.ceil(x);

System.out.println(y); // Outputs: -3.0

In this example, the value of x is -3.7, which is between -4 and -3. The Math.ceil() method will round this value up to -3.0, which is the nearest integer that is greater than -3.7.

It's important to note that the Math.ceil() method returns a double value, even if the input value is an integer. If you want to convert the result to an integer, you can use type casting. For example:

double x = 3.7;
int y = (int) Math.ceil(x);

System.out.println(y); // Outputs: 4
Created Time:2017-11-03 00:14:48  Author:lautturi