2

I'm programming an app which uses currencies. And since the app will be used in different countries I need to support the correct currency for each user.

So I made a category for NSDecimalNumber. I want to have 2 functions:

-(NSString *) getLocalizedCurrencyString: Should give me the correct string (without currency symbol, however with the local decimal separator sign and 2 digits) and

+ (NSDecimalNumber *) getUnLocalizedDecimalNumberWithString:(NSString *)currencyString: Should give me a decimal number (I guess also with 2 digits) getting rid of the local decimal separator and using the "regular" one needed to save into coredata.

However my functions don't really work. The first one does mostly, however the 2nd one always truncates the digits. What am I doing wrong?

- (NSString *) getLocalizedCurrencyString
{
    NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setCurrencySymbol:@""];

    NSString *numberString = [numberFormatter stringFromNumber:self];
    return numberString;
}

+ (NSDecimalNumber *) getUnLocalizedDecimalNumberWithString:(NSString *)currencyString
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setCurrencySymbol:@""];

    BOOL isDecimal = [numberFormatter numberFromString:currencyString] != nil;
    if(isDecimal){
        return [NSDecimalNumber decimalNumberWithString:currencyString];
    } else {
        return [NSDecimalNumber zero];
    }
}
Wain
  • 118,658
  • 15
  • 128
  • 151
MichiZH
  • 5,587
  • 12
  • 41
  • 81
  • Why aren't you just using numbers until you want to display and then using the number formatter (only for the UI)? You shouldn't be going backwards and forwards like this... – Wain Jan 27 '14 at 15:20
  • How do you mean I'm going backwards and forward? I'm using these methods only to set and get the textfield values. – MichiZH Jan 27 '14 at 15:23

1 Answers1

4

You can use locale parameter in NSNumberFormatter to convert NSDecimalNumber to correct string:

NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setCurrencySymbol:@""];
[numberFormatter setLocale:[NSLocale currentLocale]];

If you want to convert it back you use locale parameter when you create NSDecimalNumber:

NSDecimalNumber *dNumber = [[NSDecimalNumber alloc] initWithString:numberString locale:[NSLocale currentLocale]];

Hope this is what you are after.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Yep. Exactly what I was looking for. It works. However I wonder as far as I understood the NSDecimalNumber should anyway use the current locale doesn't it? But it definitely didn't in my examples I've tried. One last question: I don't want a currency symbol. But obviously depending on the currency the whitespace between the number and the (former) currency symbol remains anyway in place. How can I get rid of this whitespace as well? – MichiZH Jan 27 '14 at 15:39
  • 1
    @MichiZH see this [post](http://stackoverflow.com/questions/15534431/nsdecimalnumber-decimalnumberwithstring-ignores-current-locale?answertab=active#tab-top) regardless your first question. If you don't want to display currency symbol you should change numberStyle property of NSNumberFormatter. – Greg Jan 27 '14 at 15:48
  • Ah I see, so I don't really need the currency style right? I can just format my own decimal number :-) – MichiZH Jan 27 '14 at 16:12