Open In App

How to Round toward negative infinity in MATLAB

Rounding a number towards infinity means rounding the number X to the nearest integer less than or equal to X. In this article, we will discuss how to round toward negative infinity in MATLAB.

Example:

Suppose X = 3.5, then result is 3
And if X = -3.5, then result is -4 



The floor() function in MATLAB can be leveraged to round the number towards negative infinity. Different syntaxes of floor() function in Matlab are

Now we will discuss the syntaxes of the above function:



Y = floor(val):




% Input vector
% Input vector
val = -3.1;
 
% Rounding the elements in vector
Y = floor(val);
 
% Printing the rounded vector
disp(Y)

 
 Output :

-4

Y = floor(X):




% Input vector
% Input vector
X = [-1.2  -0.2  -4.4  7.6  -12.0];
 
% Rounding the elements in vector
Y = floor(X)
 
% Printing the rounded vector
disp(Y)

 
 Output :

-2    -1    -5     7   -12

Y = floor(t):




% Array of duration
t = hours(5) + minutes(2:4) + seconds(1.78);
 
%  Format the array into time format
t.Format = 'hh:mm:ss.SS';
 
% Display initial duration array
disp("duration :")
disp(t);
 
% Rounding the duration array
Y1 = floor(t);
disp("Rounded duration :");
disp(Y1);

 
 Output :

duration :
   05:02:01.78   05:03:01.78   05:04:01.78

Rounded duration :
   05:02:01.00   05:03:01.00   05:04:01.00

Y = floor(t,unit):




% Array of duration
t = hours(5) + minutes(2:4) + seconds(1.78);
 
%  Format the array into time format
t.Format = 'hh:mm:ss.SS';
 
% Display initial duration array
disp("duration :")
disp(t);
 
% Rounding the duration array to the nearest minutes
% less than or equal to element
Y1 = floor(t,'minutes');
disp("Rounded duration :");
disp(Y1);

 
 Output :

duration :
   05:02:01.78   05:03:01.78   05:04:01.78

Rounded duration :
   05:02:00.00   05:03:00.00   05:04:00.00

Article Tags :