16

たとえば、プリミティブを使用して、これを実行します

if ( (x >= 6000) && (x <= 20000) )
    // do something here 

NSDecimalNumberを使用すると、これが私が持っているものです

if ( (([x compare:[NSNumber numberWithInt:6000]] == NSOrderedSame) || 
        ([x compare:[NSNumber numberWithInt:6000]] == NSOrderedDescending))
    && (([x compare:[NSNumber numberWithInt:20000]] == NSOrderedSame) || 
        ([x compare:[NSNumber numberWithInt:6000]] == NSOrderedAscending)) )
{
    // do something here
}

この比較に他の方法(より簡単でエレガント)はありますか?値をプリミティブに変換する場合、どのプリミティブを使用しますか?ここでは通貨を扱っているので、CGFloat、float、doubleは使いたくありません。または、それらを上記のものに変換した場合、誰かが精度について検証/説明できますか?

4

4 に答える 4

27
于 2012-01-27T07:03:31.647 に答える
7

NSDecimalNumber+Comparisonカテゴリコード

@interface NSDecimalNumber (Comparison)

- (BOOL)isLessThan:(NSDecimalNumber *)decimalNumber;
- (BOOL)isLessThanOrEqualTo:(NSDecimalNumber *)decimalNumber;
- (BOOL)isGreaterThan:(NSDecimalNumber *)decimalNumber;
- (BOOL)isGreaterThanOrEqualTo:(NSDecimalNumber *)decimalNumber;
- (BOOL)isEqualToDecimalNumber:(NSDecimalNumber *)decimalNumber;

@end

@implementation NSDecimalNumber (Comparison)

- (BOOL)isLessThan:(NSDecimalNumber *)decimalNumber
{
    return [self compare:decimalNumber] == NSOrderedAscending;
}

- (BOOL)isLessThanOrEqualTo:(NSDecimalNumber *)decimalNumber
{
    return [self compare:decimalNumber] != NSOrderedDescending;
}

- (BOOL)isGreaterThan:(NSDecimalNumber *)decimalNumber
{
    return [self compare:decimalNumber] == NSOrderedDescending;
}

- (BOOL)isGreaterThanOrEqualTo:(NSDecimalNumber *)decimalNumber
{
    return [self compare:decimalNumber] != NSOrderedAscending;
}

- (BOOL)isEqualToDecimalNumber:(NSDecimalNumber *)decimalNumber
{
    return [self compare:decimalNumber] == NSOrderedSame;
}

@end

適切な測定のためのユニットテスト

@interface NSDecimalNumber_Comparison_Tests : XCTestCase

@end


@implementation NSDecimalNumber_Comparison_Tests

#pragma mark - isLessThan: tests

- (void)test_isLessThan_whenGreaterThan_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6779"];

    BOOL result = [decimalNumberA isLessThan:decimalNumberB];

    XCTAssertFalse(result);
}

- (void)test_isLessThan_whenLessThan_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6781"];

    BOOL result = [decimalNumberA isLessThan:decimalNumberB];

    XCTAssertTrue(result);
}

- (void)test_isLessThan_whenEqualTo_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6780"];

    BOOL result = [decimalNumberA isGreaterThan:decimalNumberB];

    XCTAssertFalse(result);
}

#pragma mark - isLessThanOrEqualTo: tests

- (void)test_isLessThanOrEqualTo_whenGreaterThan_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6779"];

    BOOL result = [decimalNumberA isLessThanOrEqualTo:decimalNumberB];

    XCTAssertFalse(result);
}

- (void)test_isLessThanOrEqualTo_whenLessThan_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6781"];

    BOOL result = [decimalNumberA isLessThanOrEqualTo:decimalNumberB];

    XCTAssertTrue(result);
}

- (void)test_isLessThanOrEqualTo_whenEqualTo_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6780"];

    BOOL result = [decimalNumberA isLessThanOrEqualTo:decimalNumberB];

    XCTAssertTrue(result);
}

#pragma mark - isGreaterThan: tests

- (void)test_isGreaterThan_whenGreaterThan_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6779"];

    BOOL result = [decimalNumberA isGreaterThan:decimalNumberB];

    XCTAssertTrue(result);
}

- (void)test_isGreaterThan_whenLessThan_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6781"];

    BOOL result = [decimalNumberA isGreaterThan:decimalNumberB];

    XCTAssertFalse(result);
}

- (void)test_isGreaterThan_whenEqualTo_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6780"];

    BOOL result = [decimalNumberA isGreaterThan:decimalNumberB];

    XCTAssertFalse(result);
}

#pragma mark - isGreaterThanOrEqualTo: tests

- (void)test_isGreaterThanOrEqualTo_whenGreaterThan_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6779"];

    BOOL result = [decimalNumberA isGreaterThanOrEqualTo:decimalNumberB];

    XCTAssertTrue(result);
}

- (void)test_isGreaterThanOrEqualTo_whenLessThan_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6781"];

    BOOL result = [decimalNumberA isGreaterThanOrEqualTo:decimalNumberB];

    XCTAssertFalse(result);
}

- (void)test_isGreaterThanOrEqualTo_whenEqualTo_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6780"];

    BOOL result = [decimalNumberA isGreaterThanOrEqualTo:decimalNumberB];

    XCTAssertTrue(result);
}

#pragma mark - isEqualToDecimalNumber: tests

- (void)test_isEqualToDecimalNumber_whenGreaterThan_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6779"];

    BOOL result = [decimalNumberA isEqualToDecimalNumber:decimalNumberB];

    XCTAssertFalse(result);
}

- (void)test_isEqualToDecimalNumber_whenLessThan_returnsNO
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6781"];

    BOOL result = [decimalNumberA isEqualToDecimalNumber:decimalNumberB];

    XCTAssertFalse(result);
}

- (void)test_isEqualToDecimalNumber_whenEqualTo_returnsYES
{
    NSDecimalNumber *decimalNumberA = [NSDecimalNumber decimalNumberWithString:@"4.6780"];
    NSDecimalNumber *decimalNumberB = [NSDecimalNumber decimalNumberWithString:@"4.6780"];

    BOOL result = [decimalNumberA isEqualToDecimalNumber:decimalNumberB];

    XCTAssertTrue(result);
}

@end
于 2017-03-29T12:51:43.557 に答える
3

compareメソッドはNSOrderedDescendingNSOrderedAscendingまたはを返しますNSOrderedSame

代わりに、簡単に書くことができます

if ( 
    [x compare:[NSNumber numberWithInt:6000]] != NSOrderedAscending && 
    [x compare:[NSNumber numberWithInt:20000]] != NSOrderedDescending
)
{
    // do something here
}

これは少し読みやすくなっています。

于 2014-12-09T15:00:30.373 に答える
2
if(([x doubleValue]>=6000.0f) && ([x doubleValue] <=20000.0f))

これもかなり正確だと思います。

于 2012-01-27T07:24:45.350 に答える