How to remove decimal in MATLAB?


In mathematics, there are several types of numeric values such as integers (having no fractional part), floating point numbers (having fractional parts separated by a decimal), and more. In some data analysis and other computational applications, we are given floating point numbers that contain a decimal part, i.e., digits after a decimal point. But we require only integers. Hence, if you are using MATLAB as your data processing tool, then you can use it to remove decimals present in the given number.

This tutorial is primarily meant for explaining the different methods of removing decimals from a given number using MATLAB.

Remove Decimal Using the "fix" Function in MATLAB

In MATLAB, there is a built-in function named, "fix" which is used to remove the decimal part from a given floating-point number to obtain the integer part only.

Syntax

integer_number = fix(floating_point_number);

The "fix" function rounds the given floating-point number towards zero to remove the decimal part and make it integer.

Example

The following example demonstrates the MATLAB code to remove the decimal from a number using the "fix" function.

% MATLAB code to remove decimal using fix function
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part using the fix function
N1_integer = fix(N1);
N2_integer = fix(N2);
N3_integer = fix(N3);

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 4
N2 = 5.525
N2 Integer = 5
N3 = 3.971
N3 Integer = 3

The "fix" function has removed the decimal part from the number by rounding it towards zero.

Remove Decimal using the "round" Function in MATLAB

In MATLAB, there is another built-in function "round" which can is used to remove the decimal from a given floating-point number and obtain the nearest integer number.

Syntax

integer_number = round(floating_point_number);

Example

The following example demonstrates the use of the "round" function to remove the decimals from a given number.

% MATLAB code to remove decimal using round function
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part using the round function
N1_integer = round(N1);
N2_integer = round(N2);
N3_integer = round(N3);

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 4
N2 = 5.525
N2 Integer = 6
N3 = 3.971
N3 Integer = 4

It can be seen that the round function removes the decimal from a given number and rounds it to the nearest integer.

Remove Decimal Using the "floor" Function in MATLAB

In MATLAB, the "floor" function is also a built-in function which is used to round down a floating-point number to its nearest integer.

Syntax

integer_number = floor(floating_point_number);

Example

The following example demonstrates the use of the "floor" function to remove the decimals from a given number to round it down to obtain the nearest integer.

% MATLAB code to remove decimal using floor function
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part using the floor function
N1_integer = floor(N1);
N2_integer = floor(N2);
N3_integer = floor(N3);

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 4
N2 = 5.525
N2 Integer = 5
N3 = 3.971
N3 Integer = 3

It can be seen that the "floor" function removes the decimals from a given number and rounds it down to the nearest integer.

Remove Decimal Using the "ceil" Function in MATLAB

In MATLAB, the "ceil" function is a built-in function which is used to round up a floating-point number to its nearest integer.

Syntax

integer_number = ceil(floating_point_number);

Example

The following example demonstrates the use of the "ceil" function to remove the decimals from a given number to round it up to obtain the nearest integer.

% MATLAB code to remove decimal using ceil function
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part using the ceil function
N1_integer = ceil(N1);
N2_integer = ceil(N2);
N3_integer = ceil(N3);

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 5
N2 = 5.525
N2 Integer = 6
N3 = 3.971
N3 Integer = 4

It is clear that the "ceil" function removes the decimals from the input decimal number and rounds it up to the nearest integer.

Remove Decimal Using the "sprintf" Function in MATLAB

In MATLAB, we can also use the "sprintf" function to remove the decimals from a given floating-point number to make it integer.

Syntax

integer_number = sprintf(format, floating_point_number);

Here, the parameter "format" represents the format specifier of the data. To remove decimals, we use '%.f' as the format specifier in MATLAB.

Example

The following example demonstrates the use of the "sprintf" function to remove the decimals from a given number to convert it into the integer.

% MATLAB code to remove decimal using sprintf function
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part using the sprintf function
N1_integer = sprintf('%.f', N1);
N2_integer = sprintf('%.f', N2);
N3_integer = sprintf('%.f', N3);

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 4
N2 = 5.525
N2 Integer = 6
N3 = 3.971
N3 Integer = 4

In this example, we have seen that the "sprint" function can also be used to remove decimals from a given floating point number.

Remove Decimal Using the "num2str" Function in MATLAB

In MATLAB, we can use the "num2str" function to remove the decimals from a floating-point number to make it integer.

Syntax

integer_number = num2str(floating_point_number, format_specifier);

Here, to remove the decimals, we use '%.0f' as the format specifier in MATLAB.

Example

The following example demonstrates the use of the "num2str" function to remove the decimals from a given number to convert it into the integer.

% MATLAB code to remove decimal using num2str function
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part using the num2str function
N1_integer = num2str(N1, '%.0f');
N2_integer = num2str(N2, '%.0f');
N3_integer = num2str(N3, '%.0f');

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 4
N2 = 5.525
N2 Integer = 6
N3 = 3.971
N3 Integer = 4

In this example, we have seen that the "num2str" function can be used to remove decimals from a given floating point number.

Remove Decimal Using the Type Conversion in MATLAB

In MATLAB, we can also remove the decimals from a given number by using the type conversion method. In this method, we convert the data type of the number to the integer data type which removes the decimal part from the input number.

Syntax

integer_number = int32(floating_point_number);

Example

The following example demonstrates the process of removing the decimals from a given number to convert it into the integer.

% MATLAB code to remove decimal through type conversion
% Provide the input decimal numbers
N1 = 4.489;
N2 = 5.525;
N3 = 3.971;

% Remove the decimal part through type conversion
N1_integer = int32(N1);
N2_integer = int32(N2);
N3_integer = int32(N3);

% Display the input decimal numbers and obtained numbers without decimal part
disp(['N1 = ', num2str(N1)]);
disp(['N1 Integer = ', num2str(N1_integer)]);
disp(['N2 = ', num2str(N2)]);
disp(['N2 Integer = ', num2str(N2_integer)]);
disp(['N3 = ', num2str(N3)]);
disp(['N3 Integer = ', num2str(N3_integer)]);

Output

When your run this code, it will produce the following output

N1 = 4.489
N1 Integer = 4
N2 = 5.525
N2 Integer = 6
N3 = 3.971
N3 Integer = 4

In this example, we have demonstrated the type conversion method to remove decimals from a given floating point number.

Conclusion

This is all about removing decimal in MATLAB. In this tutorial, I explained how you can remove decimal part (fractional part or part on right of the decimal point) from a number. MATLAB provides different ways for removing the decimal part from a floating-point number. Here, I explained all the methods used to remove the decimal with the help of examples.

Updated on: 06-Oct-2023

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements