I'm trying to build search functionality whereby a user can search for '10' and be returned results which contain transaction amounts with '10', obviously..

Take the following dataset:

10.35 70.51 64.02

Using the following predicate statement:

transaction.amount CONTAINS[cd] 10

All of the above transactions are returned because they all either contain '1','0' or a combination of both, I only want results that contain '10' sequentially.

I have tried a number of different approaches including making the value field a string,decimal and double.

Does anyone know how I could do this using an NSPredicate? I would rather not go down the route of pulling all the records down and then using a regex or something similar.

Cheers

# Best How To :

**Using NSString**

If you store amount as a string, which I find a bit weird, the following works as you want:

```
NSArray *decimals = @[@"10.35", @"70.51", @"64.02", @"11.10"];
NSLog(@"Decimals: %@", decimals);
NSArray *tens = [decimals filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] '10'"]];
NSLog(@"Tens: %@", tens);
```

Gives the following output:

```
Decimals: (
"10.35",
"70.51",
"64.02",
"11.10"
)
Tens: (
"10.35",
"11.10"
)
```

**Using NSNumber**

If you however want to keep amount as a number you could do something like this, abusing the `description`

of `NSNumber`

:

```
NSArray *decimals = @[@(10.35), @(70.51), @(64.02), @(11.10)];
NSLog(@"Decimals: %@", decimals);
NSArray *tens = [decimals filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.description CONTAINS[cd] '10'"]];
NSLog(@"Tens: %@", tens);
```

Gives the following output:

```
Decimals: (
"10.35",
"70.51000000000001",
"64.02",
"11.1"
)
Tens: (
"10.35",
"70.51000000000001"
)
```

As you can see by the output it does not give you the desired result, so one could implement a getter on your transaction `NSManagedObject`

like:

```
- (NSString *)amountSearch {
return [NSString stringWithFormat:@"%.2f", self.amount.doubleValue];
}
```

Your predicate would then be something like:

```
transaction.amountSearch CONTAINS[cd] '10'
```