๐ŸŒ
Amyuni
amyuni.com โ€บ WebHelp โ€บ JavaScript_SDK โ€บ math โ€บ Methods โ€บ Math_ceil.htm
Math.ceil
The ceil method rounds a number upwards to the nearest integer. ... Returns the the nearest integer when rounding upwards. Member of Math.
Discussions

node.js - Is there a condition where the Math.ceil function in Javascript doesn't remove decimals from the output? - Stack Overflow
Is there a condition where the Math.ceil() function in Javascript doesn't remove decimals from the output? More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Random Number, Math.floor(...) vs Math.ceil(...) - Stack Overflow
JavaScript arrays are (basically) nothing more than objects with numeric key names and a length attribute. The closest you can get is new Array(size), but that does not initialize to 0, but to undefined. ... Also interesting to find that Math.ceil is ~90% slower than Math.floor : ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
ceil - Need the nearest integer value in javascript - Stack Overflow
So how can I check that in JavaScript. ... You're expecting 1 from 1.088 and also 1 from 0.588. Why is this? ... He needs to round it. Everything x.50 and above will go to larger number and everything under x.50 will go to smaller number. ... Add 0.5 to the number and then take the floor. ... Use Math... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Newbie here. Is using Math.ceil(Math.random()) a bad idea?
As spamhammer alludes to (but to which he gives the wrong results), the distributions will in fact be different. First, we note that Math.random() returns values in the range of [0,1). That means it includes 0, but doesn't include 1. See here . The first expression thus gives you values in the range [1, n) with a uniform distribution. So it includes 1, but not n. The second expression (yours) gives you values in that same range of [1,n) plus an additional, almost infinitesimally small chance to get a value of 0. Think about it: if Math.random() returns 0, multiplying by n also gives 0. Then taking the ceiling of that value still gives you 0. This is probably why the books give you the first expression. So no, the two expressions are not equivalent, and the first one is the correct one you want to use. More on reddit.com
๐ŸŒ r/javascript
16
0
January 19, 2012
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ math ceil() javascript
Math ceil() JavaScript- Scaler Topics
June 22, 2022 - The ceil() is a JavaScript function that returns the lowest integer value that is greater than or equal to a given number. In other words, the ceil() method produces an integer value after rounding up a number.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_ceil.asp
JavaScript Math ceil() Method
The Math.ceil() method rounds a number rounded UP to the nearest integer.
๐ŸŒ
sebhastian
sebhastian.com โ€บ javascript-math-ceil
Using JavaScript Math.ceil() method | sebhastian
December 29, 2020 - The JavaScript Math.ceil() method is a method from Math object thatโ€™s used to round a decimal number up to the closest round number.
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ implementing-math-ceil-method-javascript
Implementing the Math.ceil() Method | Reintech media
January 1, 2026 - The Math.ceil() method is a built-in JavaScript function used to round a number up to its nearest integer, effectively 'ceiling' any decimal numbers. This method can be particularly useful in a variety of scenarios, such as calculating the total ...
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ javascript-the-definitive โ€บ 0596101996 โ€บ re106.html
Math.ceil( ): round a number up โ€” ECMAScript v1 - JavaScript: The Definitive Guide, 5th Edition [Book]
August 17, 2006 - NameMath.ceil( ): round a number up โ€” ECMAScript v1SynopsisMath.ceil(x)Arguments x Any numeric value or expression.ReturnsThe closest integer greater than or equal to... - Selection from JavaScript: The Definitive Guide, 5th Edition [Book]
Author ย  David Flanagan
Published ย  2006
Pages ย  1018
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-math-ceil-method
JavaScript Math ceil() Method - GeeksforGeeks
July 15, 2024 - The JavaScript Math.ceil method is used to return the smallest integer greater than or equal to a given number.
๐ŸŒ
we web developers
lochawala.com โ€บ most-useful-javascript-math-functions
most useful javascript math function
June 1, 2021 - most useful javascript math function | abs | ceil | floor | round | sign | random | min | max | trunc | technonic | lochawala
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ math-ceilin-javascript-luc-constantin
Math ceil()in Javascript
April 9, 2023 - According to MDN web docs, the Math.ceil() function always rounds a number upwards to the next largest integer.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-math-ceil-function
JavaScript Math.ceil( ) function - GeeksforGeeks
September 9, 2024 - The JavaScript Math.ceil() function rounds a given number up to the nearest integer. It always rounds towards positive infinity, meaning it increases the number to the next whole number if it's not already an integer.
Top answer
1 of 1
2

The construct:

Math.ceil(averageCost / 10) * 10;

Does a form of rounding. It has the net effect of rounding up to the nearest whole number multiple of 10. So:

11 => 20
20 => 20
21 => 30
11.99 => 20

Math.ceil(averageCost / 10) divides by 10 and rounds up to the nearest whole number (removing all decimal portions and all ones) and then the * 10 brings it back to the same numeric range it was originally in, but without the parts removed by the rounding.

Here's an example showing a number of results:

const nums = [10,11,15,18,19,20,21,10.1,11.99,20.19];

for (let num of nums) {
     let result = Math.ceil(num/10) * 10;
     console.log("Input: ", num, ", Output: ", result);
}


Why wouldn't we just use Math.ceil? Doesn't it effectively do the same thing as the above? Is there a condition where Math.ceil doesn't work?

This combination rounds up to the nearest whole number multiple of 10 whereas Math.ceil() only rounds up to the nearest whole number. So, the two cases you ask about have different uses. Use the one that accomplishes what you want to accomplish. For a number where the integer portion is already a power of ten such as 10.3, the two would have the same output, but for any other number such as 11.3, the two would not have the same output.

Math.ceil(11.3) === 11
(Math.ceil(11.3 / 10) * 10) === 20

Is there a condition where the Math.ceil() function in Javascript doesn't remove decimals from the output?

No. There is not. It's whole function is to round up to the nearest whole number (thus removing any decimal fraction from the number).

So I'm at a loss for why I'd see a division by 10 with a subsequent multiplication of 10.

To round up to the nearest whole number multiple of 10.

๐ŸŒ
Stack Abuse
stackabuse.com โ€บ rounding-numbers-in-javascript-using-ceil-floor-and-round
Rounding Numbers in JavaScript using ceil(), floor() and round()
October 17, 2021 - The round() method can be viewed as a two-in-one method, containing both ceil() and floor(). It rounds the number to the closest integer - it either "ceils" the number, or "floors" it, based on its value: let x = 4.7 console.log(Math.round(x)) // Output: 5 let y = 4.2 console.log(Math.round(y)) // Output: 4 let z = 4.5 console.log(Math.round(z)) // Output: 5 console.log(Math.round(null)) // Output: 0
๐ŸŒ
Math.js
mathjs.org โ€บ docs โ€บ reference โ€บ functions โ€บ ceil.html
math.js | an extensive math library for JavaScript and Node.js
math.ceil(c, 1) // returns Complex 3.3 - 2.7i const unit = math.unit('3.241 cm') const cm = math.unit('cm') const mm = math.unit('mm') math.ceil(unit, 1, cm) // returns Unit 3.3 cm ... math.ceil(unit, 1, mm) // returns Unit 32.5 mm math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4] math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.3, 3.9, -4.7] floor, fix, round ยท mathjs.org โ€ข copyright ยฉ 2013-2026 jos de jong โ€ข background by waseem dahman
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript โ€บ pdf โ€บ math_ceil.pdf pdf
JavaScript Math ceil Method
This JavaScript tutorial has been designed for beginners as well as working professionals to help them understand the basic to advanced concepts and functionalities of JavaScript.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ newbie here. is using math.ceil(math.random()) a bad idea?
r/javascript on Reddit: Newbie here. Is using Math.ceil(Math.random()) a bad idea?
January 19, 2012 -

Hi, I've been learning JS for a few weeks now, and it's coming along pretty well. Anyways, I've found both the books I'm reading and all the sites I use for tutorials tell me to generate random numbers using the format Math.floor(Math.random()*n+1). Wouldn't it make more sense to just use Math.ceil(Math.random()*n)? I'm only asking because everything teaches the former, but that seems inefficient. Thanks!

๐ŸŒ
Medium
medium.com โ€บ @hampojohnpaul โ€บ understanding-math-ceil-math-round-and-math-floor-c098afc63b1e
Understanding Math.ceil(), Math.round() and Math.floor() | by Hampo JohnPaul A.C. | Medium
February 17, 2022 - The rule of Math.ceil() is take the floating point number to the next positive integer irrespective of the magnitude of the floating point number.