How do I change the number of decimal places my output has

4 views (last 30 days)
I need to run a program so it shows no decimal places. I also need to run it again with showing one decimal place
  1 Comment
James Tursa
James Tursa on 17 Jan 2017
Please show some examples for input & desired output. Do you want the data clipped or rounded etc, or do you just want the display of the numbers to be a certain way?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 17 Jan 2017
See the "fieldwidth" specification of fprintf and sprintf().
So like this:
value = sqrt(pi);
for numplaces = 0 : 5
fprintf('%.*f\n', numplaces, value);
end
Note: this rounds the values to the appropriate number of decimal places.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!