Round double value to 2 decimal

조회 수: 80 (최근 30일)
Raúl
Raúl 2013년 3월 21일
Hi all,
I'm trying to round an array of double values to 2 decimals.
This is my code:
for k=1:n,
y(k)=k*st;
y(k)= sprintf('%0.2f', y(k));
x(k) = sqrt(d^2+(2*y(k))^2)-d;
x(k)= sprintf('%0.2f', x(k));
end
I got the following error message:
In an assignment A(I) = B, the number of elements in B and I must be the same.
BTW! the value n is 10.
I want the arrays y and x with 2 decimal values. Ex: 1.23
Thanks a lot.
Raúl. In an assignment A(I) = B, the number of elements in B and I must be the same.
  댓글 수: 4
Walter Roberson
Walter Roberson 2013년 3월 22일
Are you trying to change how the number is displayed, or to assign a new value that is the old one rounded to 2 decimal digits?
Raúl
Raúl 2013년 3월 25일
Hi Walter, the second approach.
Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Tearle
Matt Tearle 2013년 3월 21일
The reason you're getting the error is that sprintf creates a char array -- in this case, a 1-by-4 char array made up of the 4 characters '1', '.', '2', and '3'. But your assignment is to a single element y(k).
Possible solutions are to index into the kth row of a char array, which you should keep separate from your double array y (otherwise you'll get another error), or to use cell arrays as Wouter suggests, or to just round everything as doubles and avoid strings altogether (as Wouter also suggests).
I'd recommend the last approach. You can always display things with sprintf or fprintf at the end, and this can be done without a for-loop: fprintf(1,'%0.2f\n',x)
  댓글 수: 2
Raúl
Raúl 2013년 3월 21일
Great, the last approach works. However, it rounds the number but it keeps the rest of the number as zeros. I would like to delete all this extra zero.
For example:
From 1.3400 leave it as a 1.34.
Thanks again.
Matt Tearle
Matt Tearle 2013년 3월 21일
I don't understand what the problem is. MATLAB, like any numerical language stores all floating-point numbers to a certain precision (in MATLAB, the default is double, which is about 16 decimal places), so rounding 1.234567... to 2 dp means -- by definition -- changing the stored value to 1.230000...
How the number is stored is different to how it's displayed. MATLAB's default display is 4 dp. If you want the number displayed in a certain format, use something like fprintf. In the special case of 2 dp, you could also use the MATLAB command format bank, which will set the Command Window display style to 2 dp instead of 4. This is a universal change until you use format to change back to a different style.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Wouter
Wouter 2013년 3월 21일
편집: Wouter 2013년 3월 21일
How about this:
a = rand(1,10);
y ={};
n=length(a);
for k = 1:n
y{k} = sprintf('%0.2f',a(k));
end
Because sprintf returns a string, you need to put it in a cell: {}.
You could also round a like this (if you do not want strings):
a = rand(1,10);
y = round(a * 100)/100; % two decimals remain; more decimals are set to 0.
  댓글 수: 1
Raúl
Raúl 2013년 3월 21일
편집: Walter Roberson 2013년 3월 22일
Hi Wouter,
I've modified in this way, but still getting error message.
w ={};
z ={};
for k=1:n,
y(k)=k*st;
w{k}= sprintf('%0.2f', y(k));
x(k) = sqrt(d^2+(2*w{k})^2)-d;
z{k}= sprintf('%0.2f', x(k));
end
The error is the same
Then, i tried with:
for k=1:n,
y{k}=k*st;
w{k}= sprintf('%0.2f', y{k});
x{k} = sqrt(d^2+(2*w{k}).^2)-d;
z{k}= sprintf('%0.2f', x{k});
end
It works, but the cell z is composed by 10 cell of 1x21char... instead of doubles in the way X.XX.
Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by