I have simple divisions between integers and I always get another integer as the answer. But I want at least three decimal places. I am using MATLAB R2022b
조회 수: 2 (최근 30일)
이전 댓글 표시
My divisions look like this,
A=B/C;
Both B and C are integers.
댓글 수: 0
채택된 답변
Les Beckham
2023년 2월 8일
편집: Les Beckham
2023년 2월 10일
How were B and C created?
If they really are integers (e.g., class int32), then that is the expected result. The default type in Matlab is double, however. So, if you didn't explicitly create them as an integer type/class they will be doubles, and you should get fractional results if the numbers aren't evenly divisible. They might display as if they were integers if they are evenly divisible, however. In this case @Sulaymon Eshkabilov's suggestion will allow you to display it differently if you like.
Examples:
B = int32(3);
C = int32(2);
A = B/C % this will be rounded to the nearest integer
whos % check the type/class of the variables
B = 3;
C = 2;
A = B/C
whos % check the type/class of the variables
B = 4;
C = 2;
A = B/C % this will display as if it was an integer but it is really a double floating point number
whos % check the type/class of the variables
format bank
A
댓글 수: 0
추가 답변 (1개)
Sulaymon Eshkabilov
2023년 2월 8일
Use format. E.g.:
B = 1;
C = 5;
A=B/C
format short
A
format long
A
format bank
A
format rat
A
format long g
A
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!