var x = A.B var value = Math.ceil(x);
In the above syntax, first, we create variable ‘x’ which contains the value “A.B” after that we create another variable ‘value’ in which we assign integer x by applying the Math.ceil property to round up a number.
We have seen the syntax to round up a number, let’s see the complete algorithm step by step to understand it in a better way −
Step-1 − First, we need a variable that will be a real number and may contain some decimal part or may not contain a decimal part.
Step-2 − Later we are going to use Math.ceil() property on the current number.
Step-3 − Math.ceil() function will return an integer, and the return value does not contain any decimal part.
Step-4 − If the number passed to Math.ceil() function is a proper integer means its decimal part is zero then it will return the same number.
Step-5 − If the number passed to Math.ceil() function is a number with a decimal part non-zero then the function will make its decimal part equal to zero and return the next number to the remaining number.
In this example, we are going to round up a number using the syntax defined above.
In the above example, we have applied the Math.ceil() property to three different variables x, y, and, z. First, we create the variable ‘x’ it is an integer so when we apply the Math.ceil property to it and assign that value to variable ‘value1’ then we print that value with the help of the document.write and it returns x.
Similarly, we have done the same with variables y and z. They both are decimal numbers so they return y+1 and z+1 respectively after applying the property Math.ceil to them.
Note − Similar to the ceil() function we have floor() function. As the name suggests ceil means the above the floor() means the ground and it decreases a number. If the given number is a perfect integer with zero decimal part then as ceil() function, floor() function doesn't do any operation but if the decimal part is non-zero for the number passed as the parameter, then floor() function will remove the decimal part and will return the remaining integer part.
In the above example, we have applied the Math.floor() property to three different variables x, y, and, z. First, we create the variable ‘x’ it is an integer so when we apply the Math.floor property to it and assign that value to variable ‘value1’ then we print that value with the help of the document.write and it returns x.
In decimal numbers the property returns plus one to the original number and in non-decimal numbers it returns the same as the actual number.
- Related Articles
- How do you round up a float number in Python?
- How to Round off a number?
- How to round off a number in Python?
- How to round a number to n decimal places in Java
- How to round off a floating number using Python?
- Java program to round a number
- Round a number to the nearest even number in C#
- Golang Program to round up the next highest power of 2.