Clear Filters
Clear Filters

i have a problem rounding a loop

1 view (last 30 days)
Raul Castillo
Raul Castillo on 6 Nov 2019
Commented: Steven Lord on 7 Nov 2019
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 Comments
Shubham Gupta
Shubham Gupta on 7 Nov 2019
Edited: Shubham Gupta on 7 Nov 2019
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 on 7 Nov 2019
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

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!