NSNumberFormatter setFormat error - ios

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

Related

NSNumberFormatter for 2 Decimal Places

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:#""];

NSNumberFormatter numberFromString decimal number

I'm trying to parse a NSString with a NSNumberFormatter like following.
NSNumberFormatter *myFormatter = [[[NSNumberFormatter alloc] init];
NSNumber *myNumber = [myFormatter numberFromString:#"42.00000"];
numberFromString returns a NSNumber object in the simulator but not on a device.
The decimals (.00000) are causing the return value to be nil on a device because parsing 42 (without the decimals) works just fine (both in the simulator and on a device).
The reason I'm using a NSNumberFormatter is because is like how it returns nil if the string is not a valid number (which is working against me here :p). NSString doubleValue does not provide this kind of behaviour. Also, NSDecimalNumber decimalNumberWithString doesn't do the job because [NSDecimalNumber decimalNumberWithString:#"4a2.00000"] returns 4.
Any ideas why this would not work on a device?
Is it the locale? I tried setting myFormatter.numberStyle = NSNumberFormatterNoStyle and NSNumberFormatterDecimalStyle but it changes nothing.
As #rmaddy already said in a comment, the decimal separator of NSNumberFormatter is
locale dependent. If you have a fixed input format with the dot as decimal separator,
you can set the "POSIX locale":
NSNumberFormatter *myFormatter = [[NSNumberFormatter alloc] init];
[myFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
NSNumber *myNumber = [myFormatter numberFromString:#"42.00000"];
Alternatively, you can use NSScanner to parse a double value, as e.g. described
here: parsing NSString to Double
42.00000 is not a string mate, why not #"42.00000"?

Converting double to string return strange value in objective c

I have an NSDictionary which consist of multiple key/pair values. One of them consist double value.
NSNumber *goalValue = [info objectForKey:#"goalValue"];
I put breakpoint and I found that goalValue store the normal value that I need.
and just below I convert it to NSSting like
NSString *stringValue=[goalValue stringValue];
and this stringValue store very strange value.
Guys please help me. I am totally puzzled, I did goggle but nothing change. Please help me. Thanks in advance.
The method stringValue will convert the NSNumber to string by internally calling descriptionWithLocale: with locale as nil and this method in turn will call initWithFormat:locale:,
From Apple docs,
To obtain the string representation, this method invokes NSString’s initWithFormat:locale: method, supplying the format based on the type the NSNumber object was created with:
So format specifier used for double is %0.16g(i.e. 16 digit precision) hence the value 98.09999999999999
I'd suggest using NSNumberFormatter,
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2]; //2 decimal places, change this as required.
NSString *string = [numberFormatter stringFromNumber:goalValue];
Hope that helps!
To prevent this behavior, I suggest using NSDecimalNumber (also this is from my experience best format when dealing with very precise amounts)
NSDecimalNumber *doubleDecimal = [[NSDecimalNumber alloc] initWithDouble:[info objectForKey:#"goalValue"]];
for two digits formatting, use numberFormatter
NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
[nf setMinimumFractionDigits:2];
[nf setMaximumFractionDigits:2];
NSString *stringValue = [nf stringFromNumber:doubleDecimal]
Its showing the rounded value so you can round the value to single digit using NSNumberFormatter.
NSNumberFormatter *fomatter = [[NSNumberFormatter alloc] init];
[fomatter setMaximumSignificantDigits:2];
NSString *stringValue=[fomatter stringFromNumber:goalValue];

Formatting a number in ios

I am trying to find a solution to add zeros in the beginning of the number as per the total input provided.
Example:
Number = 100
Total Number of digits = 3
The kind of format i will like to have is 001,002,003 and so on.
Thanks
I found out the solution for the same. Posting it below:
while (totalNumCopy) {
totalNumCopy = totalNumCopy/10;
noOfDigits++;
}
NSMutableString *thumbName = nil;
if(noOfDigits > 0)
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatWidth:noOfDigits];
[formatter setPaddingCharacter:#"0"];
[formatter setNumberStyle: NSNumberFormatterPadBeforePrefix];
NSString *stringNumber = [formatter stringFromNumber:[NSNumber numberWithInt:i+1]];
thumbName = [NSString stringWithFormat:#"PageThumb%#.png",stringNumber];
[formatter release];
}
the basic string formatter is like this:
NSLog(#"%03d, %03d, %03d", 1, 2, 3);
the result would be:
001, 002, 003
maybe it helps on you.
NSString *myNumber = [NSString stringWithFormat:#"%03d", number];
i think the above code will help you

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