How to round the decimals to required digits.
Afficher commentaires plus anciens
If the values of a in double is
a=55.619999999999999;
I want to use "round" function i.e result = round(a,4); now my expected result should be result = 55.6199.
But round function is not working in MATLAB Got the below error
>> a=55.619999999999999
a =
55.619999999999997
>> round(a,4) Error using round Too many input arguments.
Any talented guys please help out.
Thank you in advance.
3 commentaires
KSSV
le 9 Juin 2017
round doesn't accept, any input other then single number. I think you should look into format.
By the way, you want to display that in file or command window?
Thouheed Shaik
le 9 Juin 2017
Modifié(e) : Walter Roberson
le 9 Juin 2017
Walter Roberson
le 9 Juin 2017
You are looking at the documentation for a release newer than what you have.
"and use it for setting flags exactly at 55.6199"
You will need to switch to the symbolic toolbox for that. MATLAB does not represent numbers in decimal. 55.6199 does not exist exactly in IEEE 754 binary double precision numbers.
Réponse acceptée
Plus de réponses (1)
John
le 9 Juin 2017
0 votes
Hi,
I think the rounded value (4 digits) for a would be a = 55.6200 (due to the trailing nines. If you want to dump the rest of the nines (truncate) to get a = 55.6299 you could use:
fix(a*1000)/1000
where a number of zeroes are the decimal points you want. Taken from https://se.mathworks.com/matlabcentral/answers/234470-truncating-a-number-without-rounding
Check this old post: https://se.mathworks.com/matlabcentral/answers/24638-round-towards-even-in-sprintf
I hope it helps...
1 commentaire
Thouheed Shaik
le 9 Juin 2017
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!