NSNumberFormatter for 2 Decimal Places - ios

I have some amount that i want to Display in local Style.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
numberFormatter.locale = [NSLocale currentLocale];
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
numberFormatter.usesGroupingSeparator = YES;
NSString* amountText = [NSString stringWithFormat:#"ab %#€",[numberFormatter stringFromNumber:someDoubleNumber]];
Problem is that this is also displaying the currency and that is something that i don't want. Basically what i am looking for is displaying that decimal should be according to local Style with 2 numbers after decimal for example 0.00 or in germany 0,00 with my code i am achieveing this requirement but i am also getting the additionally the currency symbol which i don't want.

Anybody still looking for an answer i achieved it by
[numberFormatter setCurrencySymbol:#""];

Related

NSNumberFormatter only formats up to 14 significant digits

I saw some code trying to format lat long using the following NSNumberFormatter.
NSNumberFormatter * sut = [[NSNumberFormatter alloc] init];
[sut setMaximumFractionDigits:20]; // also tried set to 15 or 16, not working too.
[sut setMaximumSignificantDigits:20];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"];
[sut setLocale:enUSPOSIXLocale];
NSString *expected = #"1.299129258067496"; // 16 significants digits
NSString *actual = [sut stringFromNumber:#(1.299129258067496)];
[[actual should] equal:expected];// Failed to pass, the actual is #"1.2991292580675"
Although in this case, we may not need to use the NSNumberFormatter to get the correct result, I'm wondering why NSNumberFormatter only returns string of up to 14 significant digits.
It only shows 14 decimal places because the double type rounds at 15 decimal places. This worked for me because you can set the number of decimal places shown.
[NSString stringWithFormat:#"%.20f", 1.299129258067496]
Just make sure the number of decimal places does not exceed the number otherwise the program makes up numbers to fill the rest. Hope this helps.

NSNumberFormatter glitch

I have a strange problem. It only seems to present on a tester's iPhone 5s.
It works correctly on an iPhone 5, 6 and 6 plus, all running the latest iOS (8.3).
This is the code
-(NSString *) commaString:(double)number
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
[numberFormatter setGroupingSize:3];
[numberFormatter setMaximumSignificantDigits:9];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble: number]];
return numberAsString;
}
The application is a calculator and is usually presenting correctly like this
It is usually showing correctly like this
but on this iPhone 5s it is showing like this
I thought the setMaximumSignificantDigits would have nipped this in the bud, but it's still showing the same. Could this be some strange localisation thing? I don't think it is as his iPhone 6 it is showing correctly also.
Thanks
Luke
If your goal is to simply have commas every three characters, all you need to do is:
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber * numberFromString = [numberFormatter numberFromString:currentTextWithoutCommas];
NSString * formattedNumberString = [numberFormatter stringFromNumber:numberFromString];
If you're finding that there's an issue with localizing the formatter, then set secondaryGroupingSize to 3. As written in the docs for NSNumberFormatter:
Some locales allow the specification of another grouping size for larger numbers. For example, some locales may represent a number such as 61, 242, 378.46 (as in the United States) as 6,12,42,378.46. In this case, the secondary grouping size (covering the groups of digits furthest from the decimal point) is 2.
Also, you can check out Formatting Numbers on iOSDeveloperTips for a brief, high-level overview.

Formatting a string containing a number separated by comma in ios

I have one double value. I have to display a double value separated by a commas in a UILabel. But instead of commas i got dot. Here is my code.
double totalCost = [abcCost doubleValue] + [defCost doubleValue];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
numberFormatter.locale = [NSLocale currentLocale];// this ensures the right separator behaviour
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
numberFormatter.usesGroupingSeparator = YES;
NSNumber *totalCostNum = [NSNumber numberWithDouble:totalCost];
NSString *totCostStr = [numberFormatter stringFromNumber:totalCostNum];
NSLog(#"%#", totCostStr);//123,345.46 prints
costLabel.text = [NSString stringWithFormat:#"$ %#", totCostStr];
while display that value in UILabel it shows 123.345.46. I want to display the value in this format 123,345.46.
Thanks in advance.
The above code is correct. I had done a small mistake. UILabel height is small, that's why it seems to be a dot. When i increased the height it displays comma.

NSNumberFormatter setFormat error

Working on my first iOS app. I think that this code should work, but I get the following error. Maybe I am missing an import or something?
No visible #interface for 'NSNumberFormatter' declares the selector'setFormat'
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setFormat:#"#0.00"];
You cann't call setFormat: for NSNumberFormatter.
Some sample code:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]];
NSLog(#"Formatted number string:%#",string);
numberStyle enum:
enum {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle
};
reslut for each numberStyle:
[1243:403] Formatted number string:123456789
[1243:403] Formatted number string:123,456,789
[1243:403] Formatted number string:¥123,456,789.00
[1243:403] Formatted number string:-539,222,988%
[1243:403] Formatted number string:1.23456789E8
[1243:403] Formatted number string:one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine
You can't set the format as you provided here. Set the number format as given below
[formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
//or other styles like NSNumberFormatterDecimalStyle,NSNumberFormatterPercentStyle
or you can create a custom number format as
[formatter setPositiveFormat:#"#0.00"];
A nice tutorial is given here
http://useyourloaf.com/blog/2012/06/14/using-number-formatters.html

Suppressing unnecessary zeros

I would like to make a string with stringWithFormat from a double value, without the unnecessary zero at the end.
Examples:
[NSString stringWithFormat:#"%.8f",2.344383933];
[NSString stringWithFormat:#"%.8f",2.0];
expected results:
2.344383933
2
Which is the correct format ?
Thank you.
Use NSNumberFormatter
[numberFormatter numberFromString:[NSString stringWithFormat:#"%.8f",0]]
Sample:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSLog(#"1: %#",[numberFormatter numberFromString:[NSString stringWithFormat:#"%.8f",2.344383933]]);
NSLog(#"2: %#",[numberFormatter numberFromString:[NSString stringWithFormat:#"%.8f",2.0]]);
Results:
1: 2.344383933
2: 2
There is a dedicated class for number formatting, NSNumberFormatter:
let formatter = NSNumberFormatter()
formatter.maximumFractionDigits = 8
print("\(formatter.stringFromNumber(2.344383933))")
print("\(formatter.stringFromNumber(2.0))")
NSNumberFormatter will also bring localization (decimal points, grouping separators).

Resources