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.

採用された回答

Les Beckham
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
A = int32 2
whos % check the type/class of the variables
Name Size Bytes Class Attributes A 1x1 4 int32 B 1x1 4 int32 C 1x1 4 int32 cmdout 1x33 66 char
B = 3;
C = 2;
A = B/C
A = 1.5000
whos % check the type/class of the variables
Name Size Bytes Class Attributes A 1x1 8 double B 1x1 8 double C 1x1 8 double cmdout 1x33 66 char
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
A = 2
whos % check the type/class of the variables
Name Size Bytes Class Attributes A 1x1 8 double B 1x1 8 double C 1x1 8 double cmdout 1x33 66 char
format bank
A
A =
2.00

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 8 日
Use format. E.g.:
B = 1;
C = 5;
A=B/C
A = 0.2000
format short
A
A = 0.2000
format long
A
A =
0.200000000000000
format bank
A
A =
0.20
format rat
A
A =
1/5
format long g
A
A =
0.2

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by