Frage

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!

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top