检查NSDecimalNumber是否为整数

时间:2012-09-06 11:09:02

标签: iphone objective-c ios nsdecimalnumber

检查NSDecimalNumber的实例是否为整数的最简单方法是什么?

谢谢!

7 个答案:

答案 0 :(得分:10)

@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

答案 1 :(得分:3)

从十进制数中减去整数。如果差值为零,则十进制数是整数。

// testVal is NSDecimalNumber
NSString *test = [testVal stringValue];
int intVal = [test intValue];
double doubleVal = [test doubleValue];
if(doubleVal-intVal == 0){
    NSLog(@"Whole number!");
}

或者,获取整数并与原始值进行比较:

if(intVal == doubleVal){
    NSLog(@"Whole number!");
}

答案 2 :(得分:3)

这应该有用。

谨防使用将NSDecimalNumber转换为double的方法。

NSDecimalNumber* input = [...];

NSDecimal roundedDecimal;
NSDecimalRound(&roundedDecimal, [input decimalValue], 0, NSRoundPlain);

NSDecimalNumber* roundedNumber = [NSDecimalNumber decimalNumberWithDecimal:roundedDecimal];

BOOL inputIsWholeNumber = [input isEqualToNumber:roundedNumber];

您还应该检查NaN值。

答案 3 :(得分:0)

你能否使用if((number%1)== 0)

答案 4 :(得分:0)

@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];

答案 5 :(得分:0)

不确定原因,但出于某种原因,我无法让Jody Hagins或Sulthan的解决方案工作,所以我的方式略有不同:

-(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;
}

答案 6 :(得分:-3)

试试这个,你可以跳过第一行,我只是用它来创建一个NSDecimalNumber对象

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");