If Statement to identify integer

조회 수: 10 (최근 30일)
Julissa Pou
Julissa Pou 2020년 11월 18일
편집: Setsuna Yuuki. 2020년 11월 18일
How do I write an if statement that checks if N is an integer and if it is not then you use ceil function to round it.

답변 (1개)

Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 18일
편집: Setsuna Yuuki. 2020년 11월 18일
N is your number.
if(mod(N,1) == 0)
fprintf("i% is a integer \n", N);
else
N = ceil(N)
end
  댓글 수: 4
Bruno Luong
Bruno Luong 2020년 11월 18일
I think isinteger does not do what you expect Timo
>> isinteger(3)
ans =
logical
0
Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 18일
편집: Setsuna Yuuki. 2020년 11월 18일
mod(N,1)
Find the modulus of N divided by 1.
if mod (N, 1) = 0, N is an integer since there is no remainder.
for example:
mod(2,1) % ---> 0
2/1 = 2 (remainder 0)
mod(2.3,1) % ---> 0.3
2.3 / 1 = 2 (remainder 0.3)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by