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];
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]]);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With