What's the easiest way to check if an instance of NSDecimalNumber
is a whole number?
Thanks!
What's the easiest way to check if an instance of NSDecimalNumber
is a whole number?
Thanks!
@interface NSDecimalNumber (IsIntegerNumber)
@property (readonly) BOOL isIntegerNumber;
@end
@implementation NSDecimalNumber (IsIntegerNumber)
-(BOOL)isIntegerNumber {
NSDecimalValue value = [self decimalValue];
if (NSDecimalIsNotANumber(&value)) return NO;
NSDecimal rounded;
NSDecimalRound(&rounded, &value, 0, NSRoundPlain);
return NSDecimalCompare(&rounded, &value) == NSOrderedSame;
}
@end
Subtract the whole number from the decimal number. If the difference is zero then the decimal number is a whole number.
// testVal is NSDecimalNumber
NSString *test = [testVal stringValue];
int intVal = [test intValue];
double doubleVal = [test doubleValue];
if(doubleVal-intVal == 0){
NSLog(@"Whole number!");
}
Or, get the whole number and compare to the original value:
if(intVal == doubleVal){
NSLog(@"Whole number!");
}
double
cannot be used with NSDecimalNumber
. What if the number has 30 digits? –
Siesta This should work.
Beware of using methods that convert NSDecimalNumber
into double
.
NSDecimalNumber* input = [...];
NSDecimal roundedDecimal;
NSDecimalRound(&roundedDecimal, [input decimalValue], 0, NSRoundPlain);
NSDecimalNumber* roundedNumber = [NSDecimalNumber decimalNumberWithDecimal:roundedDecimal];
BOOL inputIsWholeNumber = [input isEqualToNumber:roundedNumber];
You should also check for NaN
values.
@implementation NSDecimalNumber (isWholeNumber)
-(BOOL)isWholeNumber
{
double x = [self doubleValue]; //The approximate value of the receiver as a double.
double y = floor (x);
if(fabs(x - y) < DBL_EPSILON)
return YES;
return NO;
}
@end
// [myDecNumber isWholeNumber];
double
cannot cover all the values from NSDecimalNumber
. –
Siesta NSDecimalNumber
to double
or float
. I could be mistaken about this, but I'm curious to know if the outcome of this approach would be affected if that were indeed the case. Thanks. –
Sekofski NSDecimalNumber
can hold up to 38 decimal digits while double
ony about 16 decimal digits! –
Siesta Not sure why, but for some reason I was unable to get Jody Hagins' or Sulthan's solutions to work so I went a slightly different way:
-(BOOL)isWholeNumber:(NSDecimalNumber *)number {
NSDecimalNumber *input = number;
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:0
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
NSDecimalNumber *vOne = [NSDecimalNumber decimalNumberWithString:@"1"];
NSDecimalNumber *result = [number decimalNumberByMultiplyingBy:vOne
withBehavior:handler];
BOOL inputIsWholeNumber = [input isEqualToNumber:result];
return inputIsWholeNumber;
}
Try this, you can skip first line, i just used it to create a NSDecimalNumber object
NSDecimalNumber *decNumb=[NSDecimalNumber decimalNumberWithString:@"20.5"];//try with just 20
NSString* str=[NSString stringWithFormat:@"%@",decNumb];
if ([str rangeOfString:@"."].location==NSNotFound && [str rangeOfString:@"-"].location==NSNotFound) {
NSLog(@"it is whole number");
}
else
NSLog(@"it is not whole number");
© 2022 - 2024 — McMap. All rights reserved.
if ([myDecNumber isEqualToNumber:[myDecNumber integerValue]])
? – Diverticulumif ([myDecimalNumber decimalValue] / 1.0) > 0) {}
? – Dogeatdog