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

Daniel Shub
Daniel Shub 2012년 5월 3일
This question gets asked a lot and I have never understand why you would want to do this. Why do you want to do this?
Vida
Vida 2014년 2월 11일
For many reasons, for one, you have many decimal points and you export to excel the data for distribution and others want to manipulate data further. You want your data to be represented 2 decimal points because it makes sense to look at. And also you want it to be precise so any further manipulation of data would not be compromised.
Kamania Ray
Kamania Ray 2015년 11월 6일
And because a lot of us are taking CS 1371 at Tech and out TAs like us to do this.
Stephen23
Stephen23 2015년 11월 11일
편집: Stephen23 2015년 11월 11일
It probably helps to understand the visualization of data as being quite distinct from its storage in memory.
Certainly it is nice to see values with two decimal digits in Excel, but this is related to displaying of data (i.e. formatting in Excel), and not to how the data is stored in memory. Changing the formatting of a cell in Excel does not change its stored value, even if it displays different numbers of decimal digits. Similarly in MATLAB it makes more sense to keep all the numeric precision possible while calculating and storing a value, and considering displaying of that value as a separate task.
Unless there is a good reasons to change the stored data value then both MATLAB and Excel can handle those extra digits without breaking sweat: rounding does not save memory or make computations faster.
Syed Ahmad Nadeem
Syed Ahmad Nadeem 2020년 4월 19일
편집: Syed Ahmad Nadeem 2020년 4월 19일
I faced a situation while working in simulink, where I get the computed difference of two nearly equal signals to be of order -0.000001..... This signal is then followed by a signum function which has to (requried logic) detect and generate the output as 0 for it, but due to this exact value consideration it detects -1 and the calculation that follows, goes wrong. Could you please help with rounding of the signals to a specific nymber of decimal points?
Walter Roberson
Walter Roberson 2020년 4월 19일
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
Gona Sai Nikhil 2020년 9월 11일
Multiply and divide by 1000 pre and post
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."
Mahaveer Singh
Mahaveer Singh 2021년 5월 18일
ans=round(X,2)
Walter Roberson
Walter Roberson 2021년 5월 18일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jos (10584)
Jos (10584) 2014년 2월 11일
편집: Stephen23 2015년 11월 11일

4 개 추천

A = [pi exp(1) 1/7]
Ndecimals = 2
f = 10.^Ndecimals
A = round(f*A)/f

추가 답변 (7개)

Walter Roberson
Walter Roberson 2012년 5월 3일

3 개 추천

Computationally it cannot be done: binary floating point arithmetic is not able to exactly represent most multiples of 0.01.
Steven Lord
Steven Lord 2016년 11월 7일

2 개 추천

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 2020년 4월 29일
편집: Vladimir Melnikov 2020년 4월 29일

2 개 추천

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 2012년 5월 3일

1 개 추천

use roundn from Mapping Toolbox
roundn(X,-2)

댓글 수: 1

Vladimir Melnikov
Vladimir Melnikov 2020년 4월 29일
roundn(1.12345,-1)
ans = 1.100000000000000
>> roundn(1.12345,-2)
ans = 1.120000000000000
>> roundn(1.12345,-3)
ans = 1.123000000000000

댓글을 달려면 로그인하십시오.

Wayne King
Wayne King 2012년 5월 3일

0 개 추천

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

Isti
Isti 2012년 5월 3일
thanks :)
Jos (10584)
Jos (10584) 2014년 2월 11일
Use round instead of ceil!

댓글을 달려면 로그인하십시오.

Prateek Sahay
Prateek Sahay 2016년 11월 7일

0 개 추천

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

Walter Roberson
Walter Roberson 2016년 11월 7일
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
Jason Garcia 2019년 2월 7일
편집: Jason Garcia 2019년 2월 7일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Simulink에 대해 자세히 알아보기

태그

질문:

2012년 5월 3일

댓글:

2021년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by