Search code examples
iphonensnumberformatter

Proper number format not coming for spanish


I am using code below to generate a number for spanish as €1.000.000,00 but I am getting €1,000,000.00. I am using es_ES as locale ID.

NSNumberFormatter *frm=[[NSNumberFormatter alloc] init];
    [frm setNumberStyle:NSNumberFormatterDecimalStyle];

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"];
    [frm setLocale: locale];
    NSString *symbol = [locale objectForKey:NSLocaleCurrencySymbol];
    [frm setCurrencySymbol:[locale objectForKey:NSLocaleCurrencySymbol]];
    NSString *formattedAmt= [frm stringFromNumber:[NSNumber numberWithDouble:[amount doubleValue]]];

Does anyone know why I am not getting proper format?

Thanks in advance!


Solution

  • You have to set NSNumberFormatterCurrencyStyle instead of NSNumberFormatterDecimalStyle and CurrencySymbol to Empty to remove the symbol of Currency.

    Working Code :

    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [currencyFormatter setCurrencySymbol:@""];
    [currencyFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"]];
    NSLog(@"Final Value :: %@", [currencyFormatter stringFromNumber:[NSNumber numberWithFloat:1000000]]);
    

    Output (As you want without Currency Symbol) :

    Final Value :: 1.000.000,00