# How to round the decimals?

1,746 views (last 30 days)
Isti on 3 May 2012
Commented: Walter Roberson on 11 Sep 2020 at 21:37
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 :)

Walter Roberson on 19 Apr 2020
round() accepts number of digits as second argument. I am not sure offhand if you can call round in a math block; certainly from a matlab function block.
Or you could use a math block to multiply by 10 to the digits, use a round block, then another math block to divide.
But no matter what you do if you stick with double then you cannot escape the fact that 0.000001 cannot be exactly represented in double and if you subtract two nearly equal values you will get fractions below that scale.
Gona Sai Nikhil on 11 Sep 2020 at 20:21
Multiply and divide by 1000 pre and post
Walter Roberson on 11 Sep 2020 at 21:37
If you are using double precision, then multiplying and dividing by 1000 just introduces round-off error
>> fprintf('%.999g\n', foo(180), foo(180)*1000/1000)
0.311554528058658519729107183593441732227802276611328125
0.31155452805865857524025841485126875340938568115234375
You do not get rounding unless you use round() or floor() or ceil(). Which is why I suggested,
"Or you could use a math block to multiply by 10 to the digits, use a round block, then another math block to divide."

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

#### 1 Comment

Marc Lalancette on 13 Oct 2015
Divide by f, not A.

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

Walter Roberson on 3 May 2012
Computationally it cannot be done: binary floating point arithmetic is not able to exactly represent most multiples of 0.01.

Steven Lord on 7 Nov 2016
As of release R2014b you can use the round function in MATLAB to round to a specific number of decimal places.

Andrei Bobrov on 3 May 2012
use roundn from Mapping Toolbox
roundn(X,-2)

#### 1 Comment

Vladimir Melnikov on 29 Apr 2020
roundn(1.12345,-1)
ans = 1.100000000000000
>> roundn(1.12345,-2)
ans = 1.120000000000000
>> roundn(1.12345,-3)
ans = 1.123000000000000

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

Prateek Sahay on 7 Nov 2016
If you want to round 1.556876 to three decimal places then multiply it with 1000 and the use round command and then again divide it by 1000. X=1.556876 X=X*1000 Means now X=1556.876 round(x) Means now X=1556.9 X=X/1000 Means now X=1.5569

#### 1 Comment

Walter Roberson on 7 Nov 2016
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.

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.

Vladimir Melnikov on 29 Apr 2020
Edited: Vladimir Melnikov on 29 Apr 2020
the easiest way:
round (X,N)
e.g:
>> round(0.12345,1)
ans = 0.100000000000000
>> round(0.12345,2)
ans = 0.120000000000000
>> round(0.12345,3)
ans = 0.123000000000000
>> doc round