In Objective C, how can I check if value is integer number like 2.000 , 3.000, 8.000 stored as a float, and not a fraction like 2.456, 3.578
-
5"a perfect number is a positive integer that is equal to the sum of its proper positive divisors" All you want to know is if it has decimals or not, am I correct?David Rönnqvist– David Rönnqvist2013-11-16 13:11:23 +00:00Commented Nov 16, 2013 at 13:11
-
You need to define whether you mean an exact integer (which is possible in IEEE float but not some other float formats) or simply something that appears to be an integer to a given level of precision.Hot Licks– Hot Licks2013-11-16 13:19:35 +00:00Commented Nov 16, 2013 at 13:19
-
@HotLicks I have float number which increases from 0.000 to 12.000. I want to fire action when number is integer like 1.000,2.000 etc.sourabhkumbhar– sourabhkumbhar2013-11-16 13:35:38 +00:00Commented Nov 16, 2013 at 13:35
-
And you increase it in what size increments?Hot Licks– Hot Licks2013-11-16 13:48:58 +00:00Commented Nov 16, 2013 at 13:48
-
3@SourabhKumbhar Adding random fractions which are not exactly representable, such as 0.012, 0.157, 0.587, is very unlikely to result in an exact integer. You should probably allow a range around each integer.Patricia Shanahan– Patricia Shanahan2013-11-16 14:53:13 +00:00Commented Nov 16, 2013 at 14:53
|
Show 1 more comment
5 Answers
I think you're asking how to find out if a number stored as a float is an integer. There are a number of techniques. Here's one:
if(fVal == floorf(fVal))
... // do something
1 Comment
Supertecnoboff
Thank you so much for this!! I have been trying for the last two days to achieve this basic functionality and failed (lol).... You are a life saver!!
float number = myNum.floatValue;
CFNumberType numberType = CFNumberGetType((CFNumberRef)myNum);
if(number == floorf(number) && (number == 1 || number == 0))
{
// It is a bool (Exceptions 1.000, 0.00000)
}
else
{
// It is some other number use numberTyoe here except kCFNumberCharType
if (numberType == kCFNumberSInt32Type) {
}
}