Currency Formatter for different Locale in iOS - iphone

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)

Related

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!
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

Format The NSNumber in Objective C

How can I convert NSNumber "1234567 " to "12,34,567"?
I have used the NSNumberFormatter for this,but couldn't get the desired result.
I am doing like this
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:1234567]];
NSLog(#":%#",formattedString);
Output: 1,234,567
Desired Output: 12,34,567
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:#"##,##,###"];
NSString *formattedNumberString = [numberFormatter
stringFromNumber:[NSNumber numberWithFloat:1234567]];
NSLog(#"formattedNumberString: %#", formattedNumberString);
[numberFormatter release];
NSNumber *number = [NSNumber numberWithInteger:1234567];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(#"%#", [formatter stringFromNumber:number]);
[formatter release];

How to convert a NSString number to a 3 digit percentage value?

I have a NSString that has the following number 0.871 and I'm trying to convert this to display like so 87.1%
I tried to use NSNumberFormatter to get that result but I can't get it to display 3 digits. What's the best way to achieve this result? Thanks.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSNumber *number = [formatter numberFromString:inputString];
[formatter release];
NSString * str = #"0.863";
NSString * newStr = [NSString stringWithFormat:#"%.1f%%", [str floatValue]*100];
If you're trying to convert a string to a number, then format the number:
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:inputString];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSString *formattedString = [formatter stringFromNumber:number];
[formatter release];
If you just want to turn "0.871" into the number 87.1:
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:inputString];
NSDecimalNumber *percentNumber = [number decimalNumberByMultiplyingByPowerOf10:2];
Just use setMinimumFractionDigits:
NSNumber *myNumber = [NSNumber numberWithFloat:0.871];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
[formatter setMinimumFractionDigits:1];
NSLog(#"%#", [formatter stringFromNumber:myNumber]); // 87.1%
this also ensures that percentages are formatted appropriately for the locale.

iPhone, how do I get the device short date format?

I've not got the device price format setting.
NSNumber *temp = [NSNumber numberWithDouble:dblPrice];
NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithDecimal:[temp decimalValue]];
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
return [currencyFormatter stringFromNumber:someAmount];
How do I get the short date setting ?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

From string with locale to date on iphone sdk

I trying to find a way to convert a string into a date with a given locale identifier.
I have for example an italian date (locale: it_IT) I want to convert into a valid date object.
NSDate *GLDateWithString(NSString *string, NSString *localeIdentifier) {
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = [formatter dateFromString:string];
[formatter release];
return date;
}
this code does not work, the date is nil.
I can't figure out how I should use the locale setting for my purpose.
The solution is to use getObjectValue:forString:range:error: method of NSDateFormatter and set the correct date and time style ad follow:
- (NSDate *)dateWithString:(NSString *)string locale:(NSString *)localeIdentifier {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = nil;
NSRange range = NSMakeRange(0, [string length]);
NSError *error = nil;
BOOL converted = NO;
converted = [formatter getObjectValue:&date forString:string range:&range error:&error];
[formatter release];
return converted? date : nil;
}
example:
NSString *italianDate = #"30/10/2010";
NSString *italianLocale = #"it_IT";
NSDate *date = [myCustomFormatter dateWithString:italianDate locale:italianLocale];
I struggled with German formats as well sometime ago and solved the problem by supplying my own formatting string:
[formatter setDateFormat:#"dd.MMM.yyyy HH:mm:ss"];
Then:
[dateFormatter dateFromString:#"01.Dez.2010 15:03:00"];
will get a correct NSDate.