Proper number format not coming for spanish - iphone

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

Related

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];
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)

Custom Number Pad with $ sign

Okay so I have a custom number pad that works and shows numbers as 0.00 in a label (numberField), now i need it to show $0.00.
NSString *digit = sender.currentTitle;
numberField.text = [numberField.text stringByAppendingString:digit];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGroupingSeparator:#""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];
numberField.text = [numberField.text stringByReplacingOccurrencesOfString:#"." withString:#""];
NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text] decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:#"100"]];
NSString *numberFieldFormat = [numberFormatter stringFromNumber:currency];
numberField.text = numberFieldFormat;
I tried a $%1.2f but it crashes because it does not recognize the $ sign as a number.
Can someone help me out here? Or have a better way of making a custom pad with $ sign?
**Edit
I'm considering making numberField a hidden label (alpha 0) and placing a copy (numberField2) directly on top and running it through stringWithFormat. It works but i thought there may be a cleaner way of doing it.
You don't want it to show $0.00. What if I'm in the UK? Wouldn't I then want it to show £0.00?
It looks like you're incorrectly using the NSNumberFormatter. Here's how you format a number for currency:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *string = [f stringFromNumber:[NSNumber numberWithFloat:1234.56]];
[f release];
That will format things correctly, regardless of your locale. It'll use the correct currency sign, the correct thousands separator, and the correct decimal separator. Attempting to recreate this functionality yourself is a Bad Idea™.
Try this:
NSString *digit = sender.currentTitle;
numberField.text = [numberField.text stringByAppendingString:digit];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGrouping Separator:#""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];
numberField.text = [numberField.text stringByReplacingOccurrencesOfString:#"." withString:#""];
numberField.text = [numberField.text stringByReplacingOccurrencesOfString:#"$" withString:#""];
NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text] decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:#"100"]];
NSString *numberFieldFormat = [numberFormatter stringFromNumber:currency];
numberField.text = [NSString stringWithFormat:#"$%#", numberFieldFormat];
From my understanding you just want to append a $ to your string. You could do something like this..
....
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGrouping Separator:#""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];
NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text] decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:#"100"]];
NSString *formatted = [[currency stringValue] stringByReplacingOccurrencesOfString:[currency stringValue] withString:[NSString stringWithFormat:#"$%#",[currency stringValue]]];
NSLog(#"%#", formatted); // Prints $0.00
Or Am I misunderstanding your question?

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];

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]];

parse number from a locale specific string?

I have to get the number from France-number-style string: #"30.000,00"
When I use NSNumberFormattern to parse
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *priceNumberValue = [numberFormatter numberFromString:#30.000"];
It returned 30, my expected return is 30000
I try to use other options
NSLocale *vnLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
[numberFormatter setLocale:vnLocale];
or
[numberFormatter setDecimalSeparator:#","];
but it still returns unexpected value: 30
What is the correct way for me to tell the NSNumberFormatter parse #"30.000,00"?
Setting the locale should work, but you'll have to tell the numberformatter to use the grouping separator:
[numberFormatter setUsesGroupingSeparator:YES];
If that doesn't work, the following will definitely work:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setDecimalSeparator:#","];
[numberFormatter setGroupingSeparator:#"."];
[numberFormatter setUsesGroupingSeparator:YES];
NSNumber *priceNumberValue = [numberFormatter numberFromString:#"30.000,00"];
After checking this, it seems that the "fr-FR" locale doesn't have the properties you expect. However, using the "fr-BE" locale works fine:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_BE"];
[numberFormatter setLocale:locale];
[locale release];
[numberFormatter setUsesGroupingSeparator:YES];
NSNumber *priceNumberValue = [numberFormatter numberFromString:#"30.000,00"];