views:

99

answers:

1

I have amount attribute in coredata model with string datatype. I need to find records by comparing amount. I have tried NSPredicate with following but nothing is coming and result is always empty.

NSDecimal result;
NSScanner *theScanner = [[NSScanner alloc] initWithString:@"10.00"];
[theScanner scanDecimal:&result];
[theScanner release];



 [request setPredicate:[NSPredicate predicateWithFormat:@"amount > %@",[NSDecimalNumber decimalNumberWithDecimal:result]]];
 NSError* error = nil;
 NSArray* records = [managedObjectContext executeFetchRequest:request error:&error];
 NSLog(@"%@",records);

Please remember that i am storing amount value in string data type in coredata. following are some examples of what i am storting into data.

Examples: -12.00, +30.00 etc.

I know storing decimal values in string type is not good, but i didn't find a good way to store amount with negative or positive mark within coredata attribute. let me if there is any good way to do that.

+1  A: 

Just use Decimal data type for amounts (corresponds with NSDecimalNumber class in Objective-C).

Diederik Hoogenboom
Thanks, I did but now the problem is i am unable to compare amount value like following. amount < 0 of model
AmitSri
I tried the following ([amount doubleValue] <= 0) and it worked.As i understood NSDecimalNumber is a structure and we can't compate it directly as i did before.Thanks
AmitSri
`NSDecimalNumber` is a subclass of NSNumber.
Diederik Hoogenboom