Effacer les filtres
Effacer les filtres

i have a problem rounding a loop

5 vues (au cours des 30 derniers jours)
Raul Castillo
Raul Castillo le 6 Nov 2019
Commenté : Steven Lord le 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 commentaires
Shubham Gupta
Shubham Gupta le 7 Nov 2019
Modifié(e) : Shubham Gupta le 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 le 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

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by