How can I reduce the number of digits?

30 ビュー (過去 30 日間)
A. Dahash
A. Dahash 2017 年 12 月 12 日
編集済み: XIAOLEI CHEN 2018 年 7 月 24 日
Hi,
while i'm running Matlab, I have to run some simulations and their results are written in a variable workspace in Matlab. However, the precision (number of digits after the decimal) is too high and I want to reduce it to 4. I tried many codes but none worked.
Any help is appreciated!
P.S. my problem is not the display format but when I rewrite or copy the results. Have a look!

回答 (2 件)

Jan
Jan 2017 年 12 月 12 日
data = round(data, 4);
  2 件のコメント
A. Dahash
A. Dahash 2017 年 12 月 12 日
編集済み: A. Dahash 2017 年 12 月 12 日
Already tried it. Didn't work. I want the number shown and written as 982.0783
Jan
Jan 2017 年 12 月 12 日
You have to distinguish, if you want to round the values or crop the output when you display the value as a string. The values are stored in binary format, but displayed in decimal format. Now remember that not all binary numbers have an exact representation in decimal format (and the other way around). A standard example is:
0.1 + 0.1 + 0.1 - 0.3
This is not 0.0 . But if you have a value of 0.2999999999999999, how would you display it "correctly" with 4 decimal places?
So either you want to round the values, 982.078300000001 might be correct, because it is the nearest existing binary number. Or you want to limit the display, than format and fprintf are required. I cannot guess how you create the shown table, but what ever you do, there is no magic limitation to 4 decimal places.

サインインしてコメントする。


XIAOLEI CHEN
XIAOLEI CHEN 2018 年 7 月 24 日
編集済み: XIAOLEI CHEN 2018 年 7 月 24 日
Please try the function "digits" which can change digits. and it is bettre to work with vpa function.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by