I want to know if I get an integer or float after division.
if (5.4 / 0.8 ==integer) // except something that would evaluate as true in this case
{
}
One way to do it is to use Mathf.Round
to find the closest integer to the result and Mathf.Approximately
to compare that integer to the result:
float f1 = 0.5;
float f2 = 0.1;
float result = f1/f2;
if (Mathf.Approximately(result, Mathf.Round(result)) {
Debug.Log("integer result");
}
Floating point number calculation comes with the precision problem.
For example, 0.3 / 0.1
equals to 2.9999999999999996, not 3.
In order to make a comparison, you'll need to round them and check if the difference is acceptable.
var result = 0.3 / 0.1;
if (Math.Abs(Math.Round(result) - result) < 0.0000001)
{
// Do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With