Filter löschen
Filter löschen

How to round the decimals?

129 Ansichten (letzte 30 Tage)
Isti
Isti am 3 Mai 2012
Kommentiert: Walter Roberson am 18 Mai 2021
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 :)
  10 Kommentare
Mahaveer Singh
Mahaveer Singh am 18 Mai 2021
ans=round(X,2)
Walter Roberson
Walter Roberson am 18 Mai 2021
Right, these days round() in MATLAB supports passing in the number of decimal digits. When the question was originally asked, that option was not available.
Also, some of the users were needing to work in Simulink, but the round block https://www.mathworks.com/help/simulink/slref/roundingfunction.html does not support giving a number of decimal digits.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 11 Feb. 2014
Bearbeitet: Stephen23 am 11 Nov. 2015
A = [pi exp(1) 1/7]
Ndecimals = 2
f = 10.^Ndecimals
A = round(f*A)/f

Weitere Antworten (7)

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

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

Vladimir Melnikov
Vladimir Melnikov am 29 Apr. 2020
Bearbeitet: Vladimir Melnikov am 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
also read
>> doc round

Andrei Bobrov
Andrei Bobrov am 3 Mai 2012
use roundn from Mapping Toolbox
roundn(X,-2)
  1 Kommentar
Vladimir Melnikov
Vladimir Melnikov am 29 Apr. 2020
roundn(1.12345,-1)
ans = 1.100000000000000
>> roundn(1.12345,-2)
ans = 1.120000000000000
>> roundn(1.12345,-3)
ans = 1.123000000000000

Melden Sie sich an, um zu kommentieren.


Wayne King
Wayne King am 3 Mai 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.
  2 Kommentare
Isti
Isti am 3 Mai 2012
thanks :)
Jos (10584)
Jos (10584) am 11 Feb. 2014
Use round instead of ceil!

Melden Sie sich an, um zu kommentieren.


Prateek Sahay
Prateek Sahay am 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 Kommentar
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.


Jason Garcia
Jason Garcia am 7 Feb. 2019
Bearbeitet: Jason Garcia am 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.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by