division of float numbers
古いコメントを表示
Hi, MATLAB is usually showed the result of float number with four digits, how we can get only two digits after the decimal point. Ex: a = 532.7589 I want to get it as a=532.76 and please I DONOT want to use fprintf('%.2f', a) Thanks in advance
回答 (1 件)
Star Strider
2017 年 10 月 29 日
9 件のコメント
Guillaume
2017 年 10 月 29 日
@Fatina, note that you're in luck that there is such a format predefined (bank). There is no way to have a custom display different than the few predefined by format. So if you wanted 3 digits for example, you would have to use fprintf.
Fatina Shukur
2017 年 10 月 29 日
Walter Roberson
2017 年 10 月 29 日
Yes? You did not ask for anything different.
Question: what output would you want for 34.00389 ? How about for 34.00000000000000710542735760100185871124267578125 ?
Star Strider
2017 年 10 月 29 日
@Walter — Thank you!
Fatina Shukur
2017 年 10 月 29 日
Walter Roberson
2017 年 10 月 29 日
Your original question has already been answered: To output with two decimal places, use "format bank".
You seem to have a new question in which you want some numbers to be output with two decimal places, and other numbers to be output with no decimal places. I am asking you to clarify your needs: in this new question, what would be the desired output for 34.00389 and for 34.00000000000000710542735760100185871124267578125 ?
Fatina Shukur
2017 年 10 月 29 日
Star Strider
2017 年 10 月 29 日
If you want what you describe, you have to use the round function in addition to format short g:
format shortg
x = [4.548934 89.0009];
x = round(x,2)
x =
4.55 89
You have to live with the constraints of the software you use.
Walter Roberson
2017 年 10 月 30 日
Now, 34.00000000000000710542735760100185871124267578125 is not an integer, but it rounds to an integer -- it is the very next representable number after 34 exactly. Should 34.00000000000000710542735760100185871124267578125 be displayed as 34 or as 34.00 ?
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!