Rounding fractions to 5 digit precision..!

Why can't I use round(x,n)...! while using it in command window, it always gives an error " Too many input arguments".
Matlab documentation has provided a help page using the same function but that doesn't work surprisingly.....I don't understand why?
Please help !! why the above round function is not working as it should be..!

Réponses (2)

Jan
Jan le 2 Avr 2013
Modifié(e) : Jan le 2 Avr 2013
The 2nd input is available for symbolic variables only, while for input of type double, there is 1 input argument only. See doc round.
Workaround:
value = round(value * 1e5) / 1e5;

2 commentaires

preeti
preeti le 1 Avr 2015
but suppose if i want single digit to the right of decimal wat shld i do?
Same principle:
value = round(value * 10) / 10;

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 2 Avr 2013

0 votes

MuPAD is the symbolic math toolbox, which represents numbers differently than basic MATLAB does.
Basic MATLAB cannot round to a specific number of decimal digits because MATLAB uses binary representations of fractions, and binary cannot accurate represent decimal digits.
0.1 decimal is 0.0,0011,0011,0011,... (infinitely repeating) in binary. This has exactly the same mathematical basis for the reason why 1/7 is 0.142857,142857,142857,1428571,... (infinitely repeating) in decimal. No matter what fixed (finite) numeric base you use, base 2, base 3, base 10, base 144, whatever, you will end up with values that require an infinite repeated fraction to represent.

Tags

Question posée :

le 2 Avr 2013

Commenté :

le 1 Avr 2015

Community Treasure Hunt

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

Start Hunting!

Translated by