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
Star Strider 2017년 10월 29일

1 개 추천

See the documentation for the format (link) function.
Specifically:
format bank
will do what you want.

댓글 수: 9

Guillaume
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
Fatina Shukur 2017년 10월 29일
The issue with the bank format, it shows all the numbers with two decimal points even if the number is an integer number i.e if I have x=34 I will get it as x=34.00
Walter Roberson
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
Star Strider 2017년 10월 29일
@Walter — Thank you!
Fatina Shukur
Fatina Shukur 2017년 10월 29일
Are you answering my question OR you have such a question as what you have written in above!?
Walter Roberson
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
Fatina Shukur 2017년 10월 29일
it is the same question, I have a set of different numbers, some are integers, and others are floats. if I set the format to bank in the command window, I will get all the numbers even the integers with two decimal points, however, I would like to get only two digits after the decimal point when I have float number, and the integer number should be displayed as it is, so s=4.548934 should be s=4.55 and n=89 should be n=89 NOT 89.00
is it clear?
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
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에 대해 자세히 알아보기

태그

질문:

2017년 10월 29일

댓글:

2017년 10월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by