Check if number is an integer

This is my first post here; correct me if I am doing anything wrong.

My question is: how can I check if number is an integer? Do I have to create a custom function or is there a built - in function for that?

If there is no built - in function, that's what I've made so far (edit: just realised I can't use floor like that):

(I am new with functions in MIT App Inventor, so didn't know how to set 'boolean' to a value. Correct me if I am doing anything wrong.)

Floor rounds input to the larger number not greater than the input so for example 24.12 results 24 , but 24.12 is not an integer. Try checking if contains . or , (in my country for example decimals are separeted with comma)

1 Like

yea, just realised that.

Is there a way to return true or false based on the result (so when I call IsInteger(numberHere) it would return true or false)?

Also how can I enable ? hints on blocks?

Try this and see if it works for you

1 Like

Thanks for the code. I will see if it works soon.

While you were not responding, I remixed your first code and come up with this:


Will this work?

Also how can I make the ? marks on blocks show up so I can know the result?

Connect companion and use Do it to debug your blocks

2 Likes

This probably works but I would prefer to use the solution that returns true / false, not a string value.
Your solution should work so I am going to mark it :white_check_mark:

Procedure returns true/false, I just used less blocks :slight_smile:

image

Oh ye, I see. Thanks for posting! (I will use your solution)

1 Like

I would also go with doing initialize local result to false. It used to return an empty string when object (number) wasn't a number.

Like this it will return false if input not number

image

Doing this is pointless, I think. Just replace initialize local result to (empty string) from the solution with initialize local result to false. Few blocks less, the same result.

1 Like

...just one less 'else', but yes, that would be optimum.

If the source of a number is a TextBox, setting it to NumbersOnly helps, providing a decimal point is used for floats, not a comma.

IsIntegerTest.aia (2.5 KB)

blocks

Or:

image

4 Likes

That's vastly superior Tim!

1 Like

Begs the question though as to why we do not have a maths block for this....

I don't know if there was ever a discussion for/against such a block. I think the argument for would be that it simplifies some things, but the argument against would mainly be to try and keep the blocks language somewhat constrained. I could imagine a good compromise would be modifying the is number? to have a dropdown where you could choose different constraints to check.

FYI, the more appropriate implementation of isInteger would be:

5 Likes

Modifying the block would be a good idea. One could also add a check to see if the number is a HEX number.

(Beating a dead horse)
No one used a test with a negative number to rule out the floor-based attempts.

There's no such thing as a hex number in App Inventor. It could be a text block that contains hexadecimal, but technically all number blocks are binary numbers rendered as decimal numbers.