필터 지우기
필터 지우기

Show limited digits numbers.

조회 수: 2 (최근 30일)
armin m
armin m 2021년 10월 15일
댓글: armin m 2021년 10월 16일
Hi i wanna numbers limited to 6 digits. For example. A=2.54357766 be show A=2.543578. Tnx
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2021년 10월 16일
Where do you want this to be shown?
armin m
armin m 2021년 10월 16일
편집: armin m 2021년 10월 16일
Command windows and every where i wanna to print. E.g .txt file from diary on mode. But my main purpose relate to calculation. I have ref. Which calculated accordding to 6 digits numbers so i think the numbers with for example 10 digits have effect on result. So i wanna decrease digits to 6 to compare my results with reference.

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

답변 (1개)

Chunru
Chunru 2021년 10월 16일
You can use fprintf with format specifier to format the print out. "doc fprintf".
A=2.54357766;
fprintf('%.6f\n', A);
2.543578
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 10월 16일
Note that doing so will not change the result. If you are concerned that "10 digits have effect on results" and you want to decrease the digits to 6 for calculation purpose, then you are not going to achieve your aims just by using fprintf()
Perhaps what you are looking for is
format long g
A=2.54357766
A =
2.54357766
B = round(A, 6)
B =
2.543578
If so, then be careful: MATLAB does not use decimal calculations, and 0.1 is not exactly reprepresentable in binary calculations, so round(,6) is not actually rounding to 6 decimal places, only to as close as it can get:
fprintf('%.999g\n', B)
2.543578000000000116642695502378046512603759765625
That is the exact binary representation that is used.
If you need calculations done in 6 decimal places, then you have three choices:
  1. Use the Symbolic Toolbox... and worry about the effect of the 5 hidden guard digits during calculations; or
  2. Calculate in integer ratios, perhaps using John D'Errico's Variable Precision Integer package; or
  3. Use the Fixed Point Toolbox
armin m
armin m 2021년 10월 16일
Tnx

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by