Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency Formatter for different Locale in iOS

I want to convert this String: 1000000 to following format: 10,00,000 but, I am getting output like this : 1,000,000 which is wrong. I need 10,00,000.

My Code :

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
         inputnumber = newString;
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencySymbol:@""];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:2];
[formatter setUsesGroupingSeparator:YES];
[formatter setCurrencyGroupingSeparator:@","];
// [formatter setCurrencyDecimalSeparator:@"."];
[formatter setGroupingSeparator:@","];
NSNumber *num = [NSNumber numberWithDouble:[inputnumber doubleValue]];
inputnumber = [formatter stringFromNumber:num];
like image 706
Developer Avatar asked Dec 27 '22 00:12

Developer


2 Answers

It's an Indian Currency Format. So, you have to set @"en_IN" as currencyFormatter Locale.

Working Code :

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencySymbol:@""];
[currencyFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"] autorelease]];
NSLog(@"Final Value :: %@", [currencyFormatter stringFromNumber:[NSNumber numberWithFloat:1000000]]);
like image 177
Bhavin Avatar answered Jan 10 '23 09:01

Bhavin


For Swift check this:

 let initalValue:NSString = "1000000"
    let currencyFormatter = NSNumberFormatter()
    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currencyFormatter.locale = NSLocale (localeIdentifier: "en_IN")
    let formattedValue = currencyFormatter.stringFromNumber(initalValue.doubleValue)
    print(formattedValue)
like image 21
Mukesh N Avatar answered Jan 10 '23 09:01

Mukesh N