# Basics – Rounding Cody Solutions | Matlab

Learn Matlab and earn a badge and skill. This section contains the Cody solution of MATLAB Basics – Rounding Cody Solution. Total 10 solved problems.

Visit the Matlab official Problems page of Basics – Rounding.

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

Get Notified Faster

## Problem 42641. MATLAB Basic: rounding

Do rounding near to zero
Example: -8.8, answer -8
``````function y = round_zero(x)
y = fix(x);
end``````

## Problem 42642. MATLAB Basic: rounding II

Do rounding nearest integer.
Example: -8.8, answer -9
``````function y = round_x(x)
y=round(x);
end``````

## Problem 42643. MATLAB Basic: rounding III

Do rounding towards minus infinity.
Example: -8.8, answer -9
``````function y = round_x(x)
y=floor(x);
end``````

## Problem 42644. MATLAB Basic: rounding IV

Do rounding towards plus infinity.
Example: -8.8, answer -8
``````function y = round_x(x)
y=ceil(x);
end``````

## Problem 2559. Check that number is whole number

Check that number is whole number Say x=15, then answer is 1. x=15.2 , then answer is 0.
``````function y = your_fcn_name(x)
y = floor(x) == x;
end``````

## Problem 2866. Matlab Basics - Rounding II

Write a script to round a variable x to 3 decimal places:
e.g. x = 2.3456 –> y = 2.346
``````function y = round_3_d_p(x)
y = round(x, 3);
end``````

## Problem 2867. Matlab Basics - Rounding III

Write a script to round a large number to the nearest 10,000
e.g. x = 12,358,466,243 –> y = 12,358,470,000
``````function y = round_ten_thou(x)
y = round(x, -4);
end``````

## Problem 2120. Rounding off numbers to n decimals

Inspired by a mistake in one of the problems I created, I created this problem where you have to round off a floating point number to n decimals. There are 2 inputs to the function: x: floating point number and n: number of decimals which need to match with the given solutions.
``````function y = myround(x,n)
y = round(x, n);
end``````

## Problem 43278. Make roundn function

Make roundn function using round.
x=0.55555
y=function(x,1)
y=1
y=function(x,2)
y=0.6
y=function(x,3)
y=0.56
``````function y = myroundn(x,n)
y = round(x, n-1)
end``````

## Problem 713. Find the maximum number of decimal places in a set of numbers

Given a vector or matrix of values, calculate the maximum number of decimal places within the input. Trailing zeros do not count as significant. No significant digits will result in an answer of 0.
For example:
x = [0.01 0.888 1.0 40.151 39.1244];

y = 4;
The maximum number of significant decimal places from the input is 4 (39.1244).
``````function y = find_max_sigdec(x)
longest = 0;
for i = 1 : length(x)
temp = strsplit(num2str(x(i)),'.');
if length(temp) > 1
result = length(temp{2});
if result > longest
longest = result
end
end
end
y = longest
end``````

Basics – Rounding Cody 10 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.

0