|
-
December 6th, 2011, 07:53 AM
#3
Re: Cleanest way to check if a number is an integer.
 Originally Posted by Nikel
What about
Code:
bool isNotInteger = (number > (float)((int)number);
You'd be casting the number to integer (removing fractions), then back to float to have the same data type for comparing. If your original number is greater than your truncated one, you have a fraction.
Would that work?
Converting a float to integer and then back to float is going to be very detrimental to performance. You are going to encounter a significant load-hit-store penalty. You should generally avoid casting floats <--> ints whenever possible.
You could try one of these two solutions:
Code:
bool isInteger(float num)
{
return num % 1 == 0;
}
bool isInteger(float num)
{
return (float)Math.Floor(num) == num;
}
I suspect the Math.Floor version will perform better, but you should check yourself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|