If Statement to identify integer

10 Ansichten (letzte 30 Tage)
Julissa Pou
Julissa Pou am 18 Nov. 2020
Bearbeitet: Setsuna Yuuki. am 18 Nov. 2020
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.

Antworten (1)

Setsuna Yuuki.
Setsuna Yuuki. am 18 Nov. 2020
Bearbeitet: Setsuna Yuuki. am 18 Nov. 2020
N is your number.
if(mod(N,1) == 0)
fprintf("i% is a integer \n", N);
else
N = ceil(N)
end
  4 Kommentare
Bruno Luong
Bruno Luong am 18 Nov. 2020
I think isinteger does not do what you expect Timo
>> isinteger(3)
ans =
logical
0
Setsuna Yuuki.
Setsuna Yuuki. am 18 Nov. 2020
Bearbeitet: Setsuna Yuuki. am 18 Nov. 2020
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)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by