1,611 views (last 30 days)

I have a number X = 0.135678

Then i just want to round it become 0.14. What to do?

Use round(X) will only give "0".

Thanks before :)

Jos (10584)
on 11 Feb 2014

Edited: Stephen Cobeldick
on 11 Nov 2015

A = [pi exp(1) 1/7]

Ndecimals = 2

f = 10.^Ndecimals

A = round(f*A)/f

Junaid
on 3 May 2012

This also works.

X = 0.135678;

Y = sprintf('%.2f',X)

You can set as many decimals as you want.

Walter Roberson
on 3 May 2012

Sign in to comment.

Steven Lord
on 7 Nov 2016

Sign in to comment.

Wayne King
on 3 May 2012

One way here is:

X = 0.135678;

format bank;

X

Another way is:

format; %just returning the formatting

X = ceil(X*100)/100;

Probably the last way is the best because you don't have to mess with the formatting.

Andrei Bobrov
on 3 May 2012

use roundn from Mapping Toolbox

roundn(X,-2)

Sign in to comment.

Prateek Sahay
on 7 Nov 2016

Walter Roberson
on 7 Nov 2016

That is what Jos's Answer does; http://www.mathworks.com/matlabcentral/answers/37262-how-to-round-the-decimals#answer_124002.

Note that the result of the round() would be 1557 not 1556.9

Note that the result will not be exact. There is no way to represent exactly 1.557 in binary floating point. The closest it gets is 1.556999999999999939603867460391484200954437255859375

This will display as 1.557 in most output modes, but it will not be exactly that value.

Sign in to comment.

Jason Garcia
on 7 Feb 2019

Edited: Jason Garcia
on 7 Feb 2019

Maybe not exactly what you're looking for, but if you are looking for ceiling or floor measurements the below is a fun way to specifiy directly how you want to bin the array/value.

X = rand(100,1); %Rand 100 elmnt vector w/ range 0-1.

n = 100; %Use 100 for the nearest tenth.

cX = discretize(X,[0:1/n:1],[0+1/n:1/n:1]); %Rounds X UP to nearest 1/N.

%OR

fX = discretize(X,[0:1/n:1],[0:1/n:1-1/n]); %Rounds X DOWN to nearest 1/N.

Sign in to comment.

Aaina
on 7 Feb 2020 at 14:44

I have a set of program using matrix/array, how can I change the program if I want to use only one value/integer:

function [y1, y2]=crossover(x1,x2,minval1,maxval1,minval2,maxval2)

alpha=rand(size(x1));

y1=alpha.*x1+(1-alpha).*x2;

y2=alpha.*x2+(1-alpha).*x1;

tmbv=round(y1(1:4)); % change only one value

val=unique(tmbv);

for k3=1:length(val)

logh=find(tmbv==val(k3));

if(length(logh)~=1)

tmbv(logh(1))=tmbv(logh(1))+1;

end

end

y1(1:4)=tmbv; % change only one value

tmbv=round(y2(1:4)); % change only one value

val=unique(tmbv);

for k3=1:length(val)

logh=find(tmbv==val(k3));

if(length(logh)~=1)

tmbv(logh(1))=tmbv(logh(1))+1;

end

end

y2(1:4)=tmbv; % change only one value

y1=[round(y1(1:4)) y1(5:end)]; % change only one value

y2=[round(y2(1:4)) y2(5:end)]; % change only one value

for k1=1:4

if(y1(k1)<minval1)

y1(k1)=minval1;

end

if(y2(k1)<minval1)

y2(k1)=minval1;

end

if(y1(k1)>maxval1)

y1(k1)=maxval1;

end

if(y2(k1)>maxval1)

y2(k1)=maxval1;

end

end

for k1=5:8

if(y1(k1)<minval2)

y1(k1)=minval2;

end

if(y2(k1)<minval2)

y2(k1)=minval2;

end

if(y1(k1)>maxval2)

y1(k1)=maxval2;

end

if(y2(k1)>maxval2)

y2(k1)=maxval2;

end

end

end

Walter Roberson
on 7 Feb 2020 at 20:26

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_77208

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_77208

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_195730

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_195730

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_321254

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_321254

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_322253

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/37262-how-to-round-the-decimals#comment_322253

Sign in to comment.