How do I create the following method?
Heres the method:
(BOOL) numberHasDecimal: (long double) n {
if (?????) // has decimal, like 16.300000 or 6453.353259
return YES;
else
return NO; // has no decimal, like 58.000000 or 9274.000000
}
if (fabsl(fmodl(n, 1.0)) > 0.0) {
// Has a decimal.
} else {
// Is an integer.
}
Keep in mind that floating point values are internally rounded in counter-intuitive ways, so a number may have a very small fractional component and still appear integral when passed through fmodl()
.
Also note that Apple's implementation of Objective-C is broken when handling long double
values, and may manifest difficult-to-track errors when you use them.