How to check if a number is a decimal or not


#1

I am making a project that needs to check it if it's a decimal or not. And I need to set a value to a value if it's a decimal (1) and not (0). Is there a way to so it?


#2

Try this. If it doesn't work i'll comment a different way.

Pull out a check else if and do a equal try either decimal = 1 or decimal = 0
Than pull out a set value block and put it above the else and have it say this: set value decimal to 1 and drag or tap another one out and put it below the else and have it say this set value decimal 0


#3

Oh wait, I didn't say a thing. I will add a value that is needed to be checked if it's a decimal or not.


#4

This post was flagged by the community and is temporarily hidden.


#5

How? I was planning like if I set 37.5 to a value, how will it detect? Sorry guys, Just want to learn.


#6

You can cheat by placing a dot between the numbers 7 and 5 to make 375 look like 37.5
So all 3 digit numbers will look like 2 digits with a decimal.
You might find this easier


#7

You check if the number is less than the whole number after it (38 in this case) and more than the whole number the decimal is on (37 in this case). Well you check if the value you're putting 37.5 in is less or more than those, anyway. Basically what seawolfwerehorse said with different and unspecific numbers.


#8

I don't think that works because I am working on a project that changes the value at one click to test if it's a decimal.


#9

How can I check the whole number higher and lower about it?


#10

Ok. Well, good luck.


#11

Thanks. Good luck for me :smile:


#12

Use Check Once if Value is smaller than 38 and put Check Once if Value is bigger than 37 inside.


#13

OK. I gave up on this project. Thank you guys for your cooperation! (Any post in this topic will not be noticed by me)


#14

This post was flagged by the community and is temporarily hidden.


#15

I think I might have a solution. Will all of your numbers be positive, or will it be a mix?


#16

Ok - Here is the solution for positive numbers.
Say the value "number" is the number you want to test if it is a decimal or not.
Set the value "numberDummy" to number.

When numberDummy>1
    Check once if numberDummy<10000
          Increase numberDummy by -1

And then in a different rule

When numberDummy<2
 Check once if numberDummy=1
      (it was not a decimal)
 else
      (it was a decimal)
 Set value numberDummy to 100000

#17

Wow! Asked about this and got a response from Samantha John, the co founder of hopscotch!
Here is what she said (Again, all her thinking, her images, her words, complete credit goes to her):

"The repeat block will round your number to an integer. So if you repeat times the original number and increment a test number, you can check if they come out equal at the end."

"As others have mentioned, for this to work the original number needs to be positive. To solve for this just multiply by -1 if the original number is less than 0."


#18

Hey @BuildASnowman! So, I am trying this by my own but it doesn't seem to work. I was getting trouble with the integer. Why is there a divide operation? Like, do you do it ([number to test] divided by [6]) or what? Like I feel that I must change something with that 6 and/or the nunber to be divided.


#19

@kenlauescuadro The 55 divided by 6 part is the only part you need to change. That was just a test number to test if it was an integer or not. Replace "55 divided by 6" with the number you want to test if it is a decimal or not.


#20

VIt's kind of fun, if we're in the mood for a puzzle, to figure-out an efficient way to do something as basic as to determine whether a number is an integer. I have a "hack" in mind that I haven't even tried. Maybe I'll code it...

[makes silly code...]

Ok, it seems to work. I'll post a link below:

lnteger Test by oio

I'm sure we can come-up with lots of ways to do this. In my case, it's a lot like the "3D" stuff, if you'll pardon me for bringing that up, where I re-purpose something in Hopscotch whose attributes I think are useful for some purpose not originally intended. In the above code I basically do this:

(1) Come-up with a number, stick it into some variable.
(2) Assign the X-position of a "dummy" helper object to be equal to the value in the chosen variable, knowing fully that positions can't be decimals, even if we tell them to be.
(3) Test to see whether the position of the helper object is exactly equal to the assigned number.
(4) If the two are equal, report "Integer!". If they're not, report "Non-Integer!"

On the other hand,... the charm of that challenge gets a little old, when you have a piece of code that would really benefit (in terms of speed and ease of coding) if the Hopscotch function palette had a simple function that is found in all the other languages that I have encountered. That is, of course, an "INT()" function that takes a number as its argument and cuts-off everything after the decimal point. Similar functions are "Round()" or "Floor()" or "Ceil()" which do the things that their names imply.

It would be trivial to code a test like:

IF x == INT(x) then...

and just get on with life.

But Hopscotch needs to take one step at a time and I should first hope that it will soon become possible to even enter decimal numbers in a straightforward way. It's the same with (1) HSB colors with color components that could be programmatically changed. Same with (2) backgrounds that don't disappear when when there's a "clear" command. Same with (3) unique clone properties. Same with the (4) "depth" of mathematical arguments. Same with (5) inverse trig functions. Same with a lot of things. Until it gets there, we just have to be resourceful and come up with "hacks" which can also be fun. Right?

EDIT: @kenlauescuadro, @GiraffeProductions, @Stradyvarious, @GysvANDRegulus, @BuildASnowman I'm sticking your @'s in here, to ensure you get a notice of this, since it seems like it might be of interest to some of you, and also get your takes on it, if any. For all I know, you may have a more clever work-around.