🌐
W3Schools
w3schools.com › java › ref_math_ceil.asp
Java Math ceil() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... System.out.println(Math.ceil(0.60)); System.out.println(Math.ceil(0.40)); System.out.println(Math.ceil(5)); System.out.println(Math.ceil(5.1)); System.out.println(Math.ceil(-5.1)); System.out.println(Math.ceil(-5.9));
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_ceil.htm
Java - Math ceil(double) Method
Following is the declaration for java.lang.Math.ceil() method ... This method returns the smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.
🌐
Programiz
programiz.com › java-programming › library › math › ceil
Java Math ceil()
The ceil() method takes a single parameter. ... Note: The returned value will be the smallest value that is greater than or equal to the specified argument. class Main { public static void main(String[] args) { // Math.ceil() method // value greater than 5 after decimal double a = 1.878;
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-ceil-method-examples
Java ceil() method with Examples - GeeksforGeeks
January 21, 2026 - The Math class is used to access the ceil() method. Variable a holds a positive decimal value, which is rounded up to the next integer. Variable b represents positive infinity, so the result remains infinity.
🌐
Tutorial Gateway
tutorialgateway.org › java-ceil-function
Java ceil Function
March 25, 2025 - In this math ceil example, we declared an Array of double types and assigned some random values. double [] CeilArray = {10.46, -15.98, 22.44, 95.9999, -4.8799, 12.8597}; Next, We used Java For Loop to iterate the Array.
🌐
CodeAhoy
codeahoy.com › java › Math-Ceil-method-JI_15
Java Math.ceil() Method with Examples | CodeAhoy
October 12, 2019 - Learn how to use Math.ceil() method to calculate ceiling of any number.
🌐
CodeGym
codegym.cc › java blog › java math › java math.ceil() method
Java Math.ceil() method
October 11, 2023 - class Main { public static void main(String[] args) { Double totalStudentsInClass = 25.0; Double flourWeightInKgs = 5.13; Double aPoundOfOxygenInLitres = 0.3977; Double startingArrayIndexInJava = 0.0; Double aSelfDrivingCar = Double.NaN; Double numberOfStarsInTheSky = Double.POSITIVE_INFINITY; // For parameter [ -1 > x < 0 ] Double x = -0.025; // using Math.ceil() method System.out.println("Total Students In Class = " + Math.ceil(totalStudentsInClass)); System.out.println("Flour Weight In Kgs = " + Math.ceil(flourWeightInKgs)); System.out.println("A Pound of Oxygen in Litres = " + Math.ceil(aP
🌐
Codecademy
codecademy.com › docs › java › math methods › .ceil()
Java | Math Methods | .ceil() | Codecademy
December 12, 2022 - The Math.ceil() method returns the double value that is a mathematical integer and is greater than or equal to the original value. ... Looking for an introduction to the theory behind programming?
🌐
Guru99
guru99.com › home › java tutorials › java math – ceil() floor() methods
Java Math – ceil() Floor() Methods
September 20, 2024 - Java Math Class provides useful methods for performing the math’s operations like exponential, logarithm, roots and trigonometric equations too. This tutorial teaches Java Math Class with examples.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › math.ceil() in java
Math.ceil() in Java - Scaler Topics
May 5, 2024 - ... Math.ceil() converts the number to the nearest integer greater than or equal to the given number; if an integer is passed as an argument, the result of Math.ceil() will be the same integer, but since Math.ceil() returns a double value the ...
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-ceil-method
Java Math.ceil() Method
October 9, 2022 - In this guide, we will discuss the ceil() method with examples. public class JavaExample { public static void main(String[] args) { double n1 = 5.55, n2 = -5.55; System.out.println(Math.ceil(n1)); System.out.println(Math.ceil(n2)); } }
🌐
Javatpoint
javatpoint.com › java-math-ceil-method
Java Math.ceil() method with Examples - Javatpoint
Java CopyOnWriteArrayList · indexOf() lastIndexOf() clone() toArray() Math.abs() Math.max() Math.min() Math.round() Math.sqrt() Math.cbrt() Math.pow() Math.signum() Math.ceil() Math.copySign() Math.nextAfter() Math.nextUp() Math.nextDown() Math.floor() Math.floorDiv() Math.random() Math.rint() Math.hypot() Math.ulp() Math.getExponent() Math.IEEEremainder() Math.addExact() Math.subtractExact() Math.multiplyExact() Math.incrementExact() Math.decrementExact() Math.negateExact() Math.toIntExact() Math.log() Math.log10() Math.log1p() Math.exp() Math.expm1() Math.sin() Math.cos() Math.tan() Math.asin() Math.acos() Math.atan() Math.sinh() Math.cosh() Math.tanh() Math.toDegrees ·
🌐
How to do in Java
howtodoinjava.com › home › java math.ceil() vs. math.floor() vs. math.round()
Java Math.ceil() vs. Math.floor() vs. Math.round()
September 6, 2023 - The most fundamental methods for ... The Math.ceil() method is primarily used when we want to ensure that a number is rounded up to the next highest integer, regardless of its decimal part....
Top answer
1 of 15
246

You are doing 157/32 which is dividing two integers with each other, which always result in a rounded down integer. Therefore the (int) Math.ceil(...) isn't doing anything. There are three possible solutions to achieve what you want. I recommend using either option 1 or option 2. Please do NOT use option 0.

Option 0

Convert a and b to a double, and you can use the division and Math.ceil as you wanted it to work. However I strongly discourage the use of this approach, because double division can be imprecise. To read more about imprecision of doubles see this question.

int n = (int) Math.ceil((double) a / b));

Option 1

int n = a / b + ((a % b == 0) ? 0 : 1); 

You do a / b with always floor if a and b are both integers. Then you have an inline if-statement which checks whether or not you should ceil instead of floor. So +1 or +0, if there is a remainder with the division you need +1. a % b == 0 checks for the remainder.

Option 2

This option is very short, but maybe for some less intuitive. I think this less intuitive approach would be faster than the double division and comparison approach:
Please note that this doesn't work for b < 0.

int n = (a + b - 1) / b;

To reduce the chance of overflow you could use the following. However please note that it doesn't work for a = 0 and b < 1.

int n = (a - 1) / b + 1;

Explanation behind the "less intuitive approach"

Since dividing two integers in Java (and most other programming languages) will always floor the result. So:

int a, b;
int result = a/b (is the same as floor(a/b) )

But we don't want floor(a/b), but ceil(a/b), and using the definitions and plots from Wikipedia:

With these plots of the floor and ceil functions, you can see the relationship.

You can see that floor(x) <= ceil(x). We need floor(x + s) = ceil(x). So we need to find s. If we take 1/2 <= s < 1 it will be just right (try some numbers and you will see it does, I find it hard myself to prove this). And 1/2 <= (b-1) / b < 1, so

ceil(a/b) = floor(a/b + s)
          = floor(a/b + (b-1)/b)
          = floor( (a+b-1)/b) )

This is not a real proof, but I hope you're satisfied with it. If someone can explain it better I would appreciate it too. Maybe ask it on MathOverflow.

2 of 15
62

157/32 is int/int, which results in an int.

Try using the double literal - 157/32d, which is int/double, which results in a double.

🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Math ceil(), floor(), round() Methods - Java Code Geeks
January 3, 2024 - Java Math Ceil Floor Round methods: Explore Math ceil, floor, and round methods. Learn precise number rounding for mathematical accuracy.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › ceil
Java Math ceil() - Round Up Value | Vultr Docs
September 27, 2024 - Delve into practical applications ... a basic example where a floating-point number needs rounding up. Use Math.ceil() to round the number to the nearest integer....
🌐
AlphaCodingSkills
alphacodingskills.com › java › note › java-math-ceil.php
Java Math ceil() Method - AlphaCodingSkills
public class MyClass { public static void main(String[] args) { System.out.println(Math.ceil(10.5)); System.out.println(Math.ceil(-10.5)); System.out.println(Math.ceil(0.5)); System.out.println(Math.ceil(-0.5)); } } ... AlphaCodingSkills is a online learning portal that provides tutorials on Python, Java, C++, C, C#, PHP, R, Ruby, Rust, Scala, Swift, Perl, SQL, Data Structures and Algorithms.
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java math ceil() method
Java Math ceil() Method
May 26, 2023 - class Main{ public static void main(String args[]) { double a = 6.3; double b = 5.0 / 0; double c = 0.0; double d = -0.0; double e = -0.15; System.out.println(Math.ceil(a)); // Input Infinity, Output Infinity System.out.println(Math.ceil(b)); // Input Positive Zero, Output Positive Zero System.out.println(Math.ceil(c)); // Input Negative Zero, Output Negative Zero System.out.println(Math.ceil(d)); // Input less than zero but greater than -1.0 // Output Negative zero System.out.println(Math.ceil(e)); } } ... Explanation : In the preceding example, the java math ceiling() method is used to demonstrate how it works.
🌐
TutorialKart
tutorialkart.com › java › java-math › java-math-ceil
Java Math.ceil() - Examples
September 12, 2013 - public class MathExample { public static void main(String[] args) { System.out.println(Math.ceil(Double.NaN)); System.out.println(Math.ceil(Double.POSITIVE_INFINITY)); System.out.println(Math.ceil(Double.NEGATIVE_INFINITY)); } } Output · NaN Infinity -Infinity · In this Java Tutorial, we learned about Java Math.ceil() function, with example programs.
🌐
Tutorialspoint
tutorialspoint.com › java › number_ceil.htm
Java - ceil() Method
November 21, 2024 - public class Test { public static void main(String args[]) { double d = -100.675; float f = -90; System.out.println(Math.ceil(d)); System.out.println(Math.ceil(f)); System.out.println(Math.floor(d)); System.out.println(Math.floor(f)); } } This will produce the following result − · -100.0 -90.0 -101.0 -90.0 · java_numbers.htm ·