i have a problem rounding a loop

1 次查看(过去 30 天)
Raul Castillo
Raul Castillo 2019-11-6
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 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by