If Statement to identify integer

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 日

0 投票

N is your number.
if(mod(N,1) == 0)
fprintf("i% is a integer \n", N);
else
N = ceil(N)
end

4 件のコメント

Timo Dietz
Timo Dietz 2020 年 11 月 18 日
Instead of mod you can use isinteger.
Julissa Pou
Julissa Pou 2020 年 11 月 18 日
What does the (N,1) do? Can you leave it as mod(N)?
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)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2020 年 11 月 18 日

編集済み:

2020 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by