Show limited digits numbers.
2 ビュー (過去 30 日間)
古いコメントを表示
Hi i wanna numbers limited to 6 digits. For example. A=2.54357766 be show A=2.543578. Tnx
2 件のコメント
回答 (1 件)
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 件のコメント
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
B = round(A, 6)
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)
That is the exact binary representation that is used.
If you need calculations done in 6 decimal places, then you have three choices:
- Use the Symbolic Toolbox... and worry about the effect of the 5 hidden guard digits during calculations; or
- Calculate in integer ratios, perhaps using John D'Errico's Variable Precision Integer package; or
- Use the Fixed Point Toolbox
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!