Rounding in MATLAB
Rounding entails modifying a number to align with the closest designated value, which could be the nearest integer or decimal position. MATLAB provides two primary rounding functions: ceil () and floor (). These functions allow you to round up and round down a number, respectively.
1: Rounding Up a Number using ceil()
The ceil() function in MATLAB performs rounding by raising a number to the closest integer that is greater than or equal to the input value, its syntax is as follows:
Here, “a” represents the number you want to round up, and Num is the result of the rounding operation.
Example
Suppose we have a variable named “Num” with a value of 7.9 and to round up this number to the nearest integer, we can utilize the ceil() function as demonstrated below:
Output
2: Rounding Down a Number using floor()
In MATLAB, rounding down a number down to the nearest integer less than or equal to the given number can be achieved by using the floor () function, whose syntax is given below:
Similar to the ceil() function, “a” represents the number you want to round down, and Num is the rounded result.
Example
Consider a variable Num assigned a value of 15.9. To round down this number to the nearest integer, we can employ the floor() function, as demonstrated below:
Output
Conclusion
Accurate numerical computations are crucial in many scientific and engineering disciplines. MATLAB provides the ceil() and floor() functions to facilitate rounding up and rounding down of numbers, respectively.