Matlab integer format for answer

조회 수: 6 (최근 30일)
shelly
shelly 2013년 2월 18일
How do I get matlab to output the answer of (2+24)/(4*3) as an integer? It keps giving the answer as a decimal
2.166666666666667

채택된 답변

Image Analyst
Image Analyst 2013년 2월 18일
Try a function, like int32(), fix(), ceil(), floor(), uint8(), int32(round()), etc.
a = (2+24)/(4*3) % Floating point.
aInteger = int32(a)
whos aInteger; % int32
aInteger = round(a)
whos aInteger; % Double
aInteger = floor(a)
whos aInteger; % Double
aInteger = ceil(a)
whos aInteger; % Double
aInteger = fix(a)
whos aInteger; % double
aInteger = uint16(a)
whos aInteger; % uint16
Note that with round(), ceil(), floor(), and fix() you will end up with a double but with no fractional part. If you want an actual integer, you have to cast to an integer data type with int32(), int16(), etc.

추가 답변 (1개)

Thorsten
Thorsten 2013년 2월 18일
Because the result is not an integer. To convert from decimal to integer, use one of these functions:
round, floor, ceil
  댓글 수: 1
Image Analyst
Image Analyst 2013년 2월 18일
Those give doubles with no fractional part.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by