Some time ago, I finished one of the Javascript projects from Chris Dixon’s series Math ceil()in Javascript is a very fun thing to work with.
I can tell you that I learned a bunch of new stuff, and I had fun, but besides that, what I found was a wonderful function that as a junior excited me.
Hi, I am Luc, a frontend developer in his 40s that is transitioning to have his dream accomplished: become a full stack developer.
Without further do, let’s get in.
Math.ceil()in Javascript gets you up in the air
Contrary to math.floor() function, JavaScript math.ceil() function opened for me a great opportunity to touch the sky… No, I am kidding, math.ceil() just rounds the numbers upward and math.floor() downwards.
But I don’t want to rush you to the end of this article already so I will briefly explain why I am so excited about this “new” (for me) map.ceil() function.
A few months back I started a tip calculator project and I needed to round up the numbers as I was keen for the projects I am building to be if not perfect at least (re)usable. But I couldn’t find the function I needed and the project remained on a shelf as I wanted to continue it later. Having this new project at hand I thought I could give myself a try and ”let the fingers dance on the keyboard “ as Danny Thompson likes to say.
What is Math.ceil()?
According to MDN web docs, the Math.ceil() function always rounds a number upwards to the next largest integer.
What is not?
Math.ceil() is not a constructor.
Back to our function, math.ceil() is the function that adjusts the decimal of a number to its nearest integer as shown in the image below.
This function gave me the possibility to understand and apply as well a way to round the numbers when it comes to calculating (in my project) the total amount of the tip. The definition would be like this: The ceil() method rounds a number upwards to the nearest integer, returning the result.
If the passed argument is an integer, the value will not be rounded.
Because the ceil() function is a static function of the Math object, it must be invoked through the placeholder object called Math.
Testing Out Math.ceil():
Example on how math.ceil() function works:
console.log(Math.ceil(32.67));
console.log(Math.ceil(8.5));
console.log(Math.ceil(-5.3));
In the example above, I have invoked the ceil() function using the Math class writing the output of the ceil() function to the web browser console so that it would be tested out.
Conclusion
Perhaps this might not be the greatest function ever in Javascript, but it did the trick and helped me understand another fundamental core of Javascript.
If you are curious about how the project looks, you can find the code here
The “JavaScript fun projects” by Chris Dixon is here.
You can find me on Twitter and LinkedIn
Until next time, I wish you a great and productive week ahead.
Originally written on September 5th, 2020.