0

With my language and region being set to 'en_US', I can save a decimal number ex: 0.55 in core data and upon loading the correct value is retrieved.

If I change my region to 'en_PT' i.e. to test out the formatting of currency amounts, saving the same number 0,55 gets rounded up to '0'

I have tested this on the simulator and device and I see the same behavior. The 'amount' field in coreData model is 'Decimal'. Is there anything specific I need to do in CoreData to support this ?

Here's different parts of my code ... saving to coreData

-(void)addPayment:(Payment*)payment withContext:(NSManagedObjectContext *)context onSuccess:(SuccessBlock)successBlock onFailed:(FailedBlock)failedBlock
{
   CDPayments* cdPayment = [NSEntityDescription insertNewObjectForEntityForName:PAYMENTS_TABLE
                                                                          inManagedObjectContext:context];
   [self copyPayment:payment intoCDPayment:cdPayment];
   [self saveDataInContext:context onSuccess:successBlock onError:failedBlock];
}

Retrieving from coreData

-(void) listOfPaymentsWithFilter:(NSPredicate*)predicate onSuccess:(SuccessNSArrayBlock)successBlock onFailed:(FailedBlock)failedBlock
{
    [self listOfAllObjectsForEntity:EntityTypePayments filterWith:predicate sortBy:nil
      onSuccess:^(NSArray *array)
             {
                 NSMutableArray *paymentsList = [[NSMutableArray alloc] init];

                 for (CDPayments* cdPayment in array)
                 {
                     Payment* tmpPayment = [[Payment alloc] init];
                     [self copyPayment:tmpPayment fromCDPayment:cdPayment];
                     [paymentsList addObject:tmpPayment];
                 }

                 successBlock(paymentsList);
             }
        onError:failedBlock];
}

Converting the user entering amount from a textfield to an NSDecimal number

+(NSDecimalNumber*) decimalNumberFromUserEnteredString:(NSString*)string
{
    NSNumber *number = [[NSNumberFormatter cachedAmountFormatter] numberFromString:string];
    NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithDecimal:[number decimalValue]];;

    return decimalNumber;
}

Here is the formatter ...

+(NSNumberFormatter*) cachedAmountFormatter
{
    if (cachedNumberFormatter == nil)
    {        
        cachedNumberFormatter = [[NSNumberFormatter alloc] init];
        cachedNumberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
        cachedNumberFormatter.lenient = YES;
        cachedNumberFormatter.minimumFractionDigits = 2;
        cachedNumberFormatter.maximumFractionDigits = 2;
        cachedNumberFormatter.locale = [NSLocale currentLocale];
    }

    return cachedNumberFormatter;
}
ArdenDev
  • 4,051
  • 5
  • 29
  • 50
  • Update your question with relevant code. – rmaddy Nov 29 '16 at 16:50
  • 1
    You save decimal data in Coredata with dot and for presentation use NSNumberFormatter, ever. – Claudio Castro Nov 29 '16 at 17:15
  • http://stackoverflow.com/questions/5406366/formatting-a-number-to-show-commas-and-or-dollar-sign – Claudio Castro Nov 29 '16 at 17:42
  • @CCastro ... thanks for the link. I have added the formatter code I'm using. Once I get the user entered string in their locale, I'm able to get the decimal values with the dot format and I save it as is in CoreData. Its just that when I retrieve the object again ... the decimal values are lost. Not sure what I am missing – ArdenDev Dec 01 '16 at 02:56
  • @rmaddy ... I've added the relevant code – ArdenDev Dec 01 '16 at 03:05
  • You dont need formatter to save, only replace ", " for "." and converter to nsnumber, when retrieve, for presetation, use formatter. – Claudio Castro Dec 02 '16 at 19:26
  • In your code, you first convert your string with comma to number, first you have to replace comma for dot in your string using [your_text_field stringByReplacingOccurrencesOfString:@"," withString@"."] – Claudio Castro Dec 02 '16 at 19:32

0 Answers0