Rounding fractions to 5 digit precision..!

11 views (last 30 days)
sakz
sakz on 2 Apr 2013
Commented: James Tursa on 1 Apr 2015
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..!

Answers (2)

Jan
Jan on 2 Apr 2013
Edited: Jan on 2 Apr 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 Comments
preeti
preeti on 1 Apr 2015
but suppose if i want single digit to the right of decimal wat shld i do?
James Tursa
James Tursa on 1 Apr 2015
Same principle:
value = round(value * 10) / 10;

Sign in to comment.


Walter Roberson
Walter Roberson on 2 Apr 2013
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

Community Treasure Hunt

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

Start Hunting!