This is probably because when you divide an Integer by an Integer you get an Integer back which has the same value as Decimal.round(RoundingMode.DOWN).

system.assertEquals(1, 8/5);
system.assertEquals(8/5, (8.0/5).round(RoundingMode.DOWN);

system.assertEquals(-1, -8/5);
system.assertEquals(-8/5, (-8.0/5).round(RoundingMode.DOWN);

system.assertEquals(2, 7/3);
system.assertEquals(7/3, (7.0/3).round(RoundingMode.DOWN);

If you know you have Integers and you want to get their ceiling, you could do something like:

public static Integer ceiling(Integer x, Integer y)
{
    return Math.ceil(Decimal.valueOf(x).divide(y, /*digits*/ 1));
}
Answer from Adrian Larson on Stack Exchange
Top answer
1 of 3
28

This is probably because when you divide an Integer by an Integer you get an Integer back which has the same value as Decimal.round(RoundingMode.DOWN).

system.assertEquals(1, 8/5);
system.assertEquals(8/5, (8.0/5).round(RoundingMode.DOWN);

system.assertEquals(-1, -8/5);
system.assertEquals(-8/5, (-8.0/5).round(RoundingMode.DOWN);

system.assertEquals(2, 7/3);
system.assertEquals(7/3, (7.0/3).round(RoundingMode.DOWN);

If you know you have Integers and you want to get their ceiling, you could do something like:

public static Integer ceiling(Integer x, Integer y)
{
    return Math.ceil(Decimal.valueOf(x).divide(y, /*digits*/ 1));
}
2 of 3
17

Integer Division

In Apex Code, similar to Java, when there are two like numeric data types (e.g. both Integers), then they are calculated in a way that returns the same data type. Integers cannot store fractions, so when you do something like 7/3, the fraction is silently discarded. As far as I know, integer division works in the same for every programming language in the world where the result of integer arithmetic is an integer.In many languages where the result of two integers being divided together results in an integer, the fraction is often dropped entirely, although exceptions do exist (some perform rounding, instead).

Parameter Promotion

You'll notice that there's no function Math.ceil that accepts an Integer. This means if you give it an integer, it will implicitly be cast to a floating point value before being processed by Math.ceil.

Arithmetic Promotion

When two numbers are operated on using the standard operators (+, -, /, *), if one of those parameters are a floating point value, the other one will also automatically become a floating point value. Similarly, if a integer and a long were involved in an operation, both numbers become long, and the return type becomes long. So, the goal is to create a situation where a floating point is returned. For example, this results in the correct result:

Integer x = Math.ceil(7.0/3).intValue();

Here, the 7.0 indicates a floating point operation. You'd also get the same effect if you did this:

Decimal x = 7;
Integer y = 3;
Integer z = Math.ceil(x/y).intValue();

This behavior is well-defined, and mimics the behavior in Java. You can read more about automatic widening conversion in the Java documentation, as well as rules about integer division. You'll find that, while not explicitly mentioned in the Apex Code Developer's Guide (as far as I can tell), it obeys the same rules.

🌐
Iqra Technology
iqratechnology.com › academy › salesforce-apex-training › math-class-in-apex
Math Class in Apex – Iqra Technology
July 29, 2025 - Write programs using the Math class methods in Apex: 1. Round the number 9.3 to the nearest integer using Math.round(). 2. Round the number 12.89 down using Math.floor(). 3. Find the absolute value of -100.50 using Math.abs(). 4. Calculate the square root of 225 using Math.sqrt().
🌐
Forcetalks
forcetalks.com › home › salesforce® discussions › what is the use of math.ceil ?
Salesforce | What is the use of Math.ceil ? - Forcetalks
April 2, 2020 - The Math.ceil() function always rounds a number up to the next largest whole number or integer.
🌐
Evisions
webhelp.evisions.com › HelpFiles › Argos › en › Content › Salesforce Connector MATH Functions.htm
Salesforce Connector MATH Functions
SELECT CEILING(1.3); -- Result: 2 · SELECT CEILING(1.5); -- Result: 2 · SELECT CEILING(1.7); -- Result: 2 · Returns the trigonometric cosine of the specified angle in radians in the specified expression. float_expression: The float expression of the specified angle in radians.
🌐
YouTube
youtube.com › watch
Math Class Methods - Apex Programming Level 5 ⭐⭐⭐⭐⭐ - YouTube
This video explains different methods of Math class in Apex.Please subscribe if you haven't subscribed to us yet - https://bit.ly/3v61lv5--------------------...
Published   November 7, 2022
🌐
Forcetalks
forcetalks.com › home › salesforce® discussions › what is the use of math.ceil in lightning component in salesforce?
What is the use of math.ceil in Lightning Component in Salesforce? - Forcetalks
April 2, 2020 - Note: Math.ceil(null) returns integer 0 and does not give a NaN error ek 2.5=3, 2=2, -7.004=-7. we also have Math.floor Math.floor() function returns the largest integer less than or equal to a given number. it can be used in a lot of ways in your lightning component, for ex;- In pagination where your total no of records are 20, and your page size is 8, to go to the last page, where page no is being calculated as totalrecords/pagesize, i this case 2.5, math.ceil(2.5) returns 3, and we have our page no · Log In to reply. ... Testing is a crucial part of the Salesforce development life cycle. Whenever we do code in Salesforce, we cannot send it directly to the production.… · Apex Test Class, Apex Triggers, Best Practices, Bugs, Bulk Tests
🌐
Blogger
hecthick.blogspot.com › 2012 › 02 › examples-of-math-methods-in-saleforce.html
Salesforce: Examples of Math methods in Saleforce
June 17, 2022 - Example of Math.ceil():- Integer WeekOfMonth = Math.ceil((Double)(system.today().Day()) / 7).intValue(); system.debug('Result:::::::::'+ WeekOfMonth); --------------------------------------------------------------------------------------------- Example for rounding decimal & double Direct method:- Decimal d = 1.426 ; Double num = d.setScale(2) ; System.debug('Result::::::::::::: ' + num) ; -------------------------------------------------------------------------------------------- Example for using power in salesforce:- Decimal myDecimal = 4.12; Decimal powDec = myDecimal.pow(2); system.debug(
Find elsewhere
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54V00007T4EzoSAF
I want to use math.ceil function if - Trailhead - Salesforce
Skip to main content · The Trailblazer Community will undergo maintenance on Saturday, November 15, 2025 and Sunday, November 16, 2025. Please plan your activities accordingly
🌐
Pathtosalesforce
pathtosalesforce.com › index.php › courses › apex-beginner
Path to Salesforce - 080 | Math.ceil(x)
{tab title="Notes"}Returns the smallest (closest to negative infinity) Decimal that is not less than the argument and is equal to a mathematical integer. Here are some examples: Decimal x = 4.75; System.debug(Math.ceil(x)); Output: 5 Decimal x = -4.75; System.debug(Math.ceil(x)); Output: -4{/tabs}
🌐
GitHub
github.com › valeoai › rainbow-iqn-apex › blob › master › rainbowiqn › env.py
rainbow-iqn-apex/rainbowiqn/env.py at master · valeoai/rainbow-iqn-apex
reward = math.ceil(reward / 100) # All reward are multiplied by 100 for no reason · · # Return state, reward, done · return list(self.state_buffer), reward, done · · def action_space(self): return len(self.actions) ·
Author   valeoai
🌐
Peterknolle
peterknolle.com › calculations-in-apex
Peter Knolle - Trifecta Technologies | LinkedIn
January 9, 2017 - ✅ A: Use HttpCalloutMock for different responses—success, failure, and timeout. Assert retry logic, status flags, and response handling. 6️⃣ Governor Limit Spike in Batch Execution Q: You wrote a Batch Apex class to process 50,000 records, but it's failing with CPU time limit exceptions.
🌐
Alteryx
help.alteryx.com › aac › en › trifacta-classic › wrangle-language › math-functions › ceiling-function.html
CEILING Function
Math Functions · CEILING Function · Computes the ceiling of a value, which is the smallest integer that is greater than the input value. Input can be an Integer, a Decimal, a column reference, or an expression. Wrangle vs. SQL: This function is part of Wrangle, a proprietary data transformation ...