I’ve looked on every website I could and they’re all explaining it in a way that I just don’t get when I refer it back to that line of code: console.log(Math.ceil(43.8));
I can type the code and get the desired result fine, I just can’t get my head around what .ceil() actually does, so if someone could please please please explain this in the most simple terms possible, that would be really appreciated!
Thank you in advance
Math.ceil() returns the next higher absolute number:
Math.ceil(1.1); // returns 2
Math.ceil(1.7); // returns 2
Math.floor() is it’s counterpart and returns the next lower absolute number:
Math.floor(1.1); // returns 1
Math.floor(1.7); // returns 1
Math.round() is what you know from Math lessons:
Math.round(1.1); // returns 1
Math.round(1.7); // returns 2
In programming, you often have use cases where you need full control over the returned number, so Math.floor and Math.ceil are frequently used. One example of a common use case would be a random number generator:
let randomNumber = Math.ceil(Math.random() * 10) // returns absolute numbers from 1 to 10
Math.random() returns decimals between 0 and less than 1 (0.9999999)
Ohhh so … Math.ceil (x);
I’m just gonna run through how I understand your explination so I can be corrected if it’s wrong lol …
If the “x” is a whole number (including 0), or a negative number (whole or decimal), then the number displayed will be the same as “x”, just without the numbers after the decimal.
In the instance that “x” is, for example, 86.013, what would be displayed would be 87…
If “x” is 41, it would display as 41 …
And if “x” is -20.3, it would be displayed just as -20 … Is that right? Am I understanding it properly?