- Home /
Get integer or float value of a variable
Hi, I am trying to find out how to get the value(integer or float) of the division I made on the variable, something like this:
var num1 = 20;
var num2 = 10;
function Update(){
var checkint = num1/num2;//checkint = 10
if(/*checkint.value == int*/){//this is what I need to know
DoSomething();
}
}
as you can see in this case checkint would be an int so it would do something else, but if lets say, num1 is 22,so the division would end in float(2.2) so it would not pass the if.
Answer by Eric5h5 · Feb 26, 2011 at 01:38 AM
See if the modulus results in 0:
if (num1 % num2 == 0) {
// is integer
}
sorry, maybe I'm not good explaining, I want to know how to make a variable to know if it's value is integer or float. I don't want to set the variable as float, I want to get or know what the variable checkint is an integer or a float.
Answer by by0log1c · Feb 26, 2011 at 06:36 AM
Eric5h5 code works but I believe it could break if you change the variables' values.
I'm not 100% certain what you're trying to achieve but if you're just making sure a number is a int(round number) and not a float. you could always compare the trueValue with a roundValue. Something like:
if( checkint == Mathf.Round(checkint) ){
DoSomething();
}
will occurs only if the checkint numeric value is an int.
On a side note, for your solution it would be better to use int(checkint) ins$$anonymous$$d of $$anonymous$$ath.Round(checkint), which is quite more expensive.