Asked by Isti
on 3 May 2012

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 :)

Answer by Jos (10584)
on 11 Feb 2014

Edited by Stephen Cobeldick
on 11 Nov 2015

Accepted Answer

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

Ndecimals = 2

f = 10.^Ndecimals

A = round(f*A)/f

Answer by Junaid
on 3 May 2012

This also works.

X = 0.135678;

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

You can set as many decimals as you want.

Daniel
on 4 Mar 2015

Thanks!

Joep
on 11 Nov 2015

Very unconventional way to use. Converting from str2num and otherwise are "slow" process. It works but it is a real meshing solution. I prefer

round(X, n)

X your number witch you want to round, n the decicmals (- behind the comma/dot)

Stevilinsko
on 21 Nov 2016

Joeps way only works with Matlab Versions starting at R2014b :/

Sign in to comment.

Answer by Walter Roberson
on 3 May 2012

Sign in to comment.

Answer by Steven Lord
on 7 Nov 2016

Sign in to comment.

Answer by 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.

Isti
on 3 May 2012

thanks :)

Jos (10584)
on 11 Feb 2014

Use round instead of ceil!

Sign in to comment.

Answer by Andrei Bobrov
on 3 May 2012

use roundn from Mapping Toolbox

roundn(X,-2)

Sign in to comment.

Answer by 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.

Answer by Jason Garcia
on 7 Feb 2019

Edited by 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.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Daniel Shub (view profile)

## Direct link to this comment

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

## Vida (view profile)

## Direct link to this comment

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

## Kamania Ray (view profile)

## Direct link to this comment

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

## Stephen Cobeldick (view profile)

## Direct link to this comment

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

Sign in to comment.