필터 지우기
필터 지우기

i have a problem rounding a loop

조회 수: 5 (최근 30일)
Raul Castillo
Raul Castillo 2019년 11월 6일
댓글: Steven Lord 2019년 11월 7일
so if i write a loop that and it gives me a answer of .0215243225 you know just a huge number but i wanted to round it to the .0001, .001, .01. how would i do that i m using a for statment for a loop
  댓글 수: 6
Shubham Gupta
Shubham Gupta 2019년 11월 7일
편집: Shubham Gupta 2019년 11월 7일
Do you want to round the value to significant digits, which will change in a loop? For e.g.
a_answer= .0215243225;
for i = 2:5
output = round(a_answer,i)
end
% You will get output
output =
0.02
output =
0.022
output =
0.0215
output =
0.02152
It's basically the same method as mentioned by John, only instead of looping on values I looped it for significant digits.
Steven Lord
Steven Lord 2019년 11월 7일
You can do that without manually adjusting the number of digits by specifying the 'significant' option in your call to round.
>> a_answer= .0215243225;
>> round(a_answer, 3, 'significant')
ans =
0.0215
>> b = pi/1000;
>> round(b, 3, 'significant')
ans =
0.00314
>> round(b, 5)
ans =
0.00314

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by