iosin-app-purchasecurrencynslocale

Correct in app-purchasing local currency symbol


With in app purchasing I want the local currency symbol to obviously suit the users local currency. Is it safe to use NSLocaleCurrencySymbol as the main source of detection of where the user is. Heres part of my code:

         NSLocale *theLocale = [NSLocale currentLocale];
     NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
     NSString *cost = [NSString stringWithFormat:@"%@",product.price];

By default is the correct Region Format automatically done in the International settings on each device when a user purchases an iOS device ??


Solution

  • You want to use a NSNumberFormatter:

    Objective-C

    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setLocale:product.priceLocale]; 
    NSString *cost = [formatter stringFromNumber:product.price];
    

    Swift

    let formatter = NSNumberFormatter()
    formatter.numberStyle = .CurrencyStyle
    formatter.locale = product.priceLocale
    let cost = formatter.stringFromNumber(product.price)
    

    This will then format the currency with the correct decimalisation, separator and currency symbol.