Reputation: 1632
I am having an issue with an NSNumber variable "num" that is currently containing 0.522000003606
I looked up several other issues on stackoverflow with others having the same issue but nothing. My project is currently using ARC so I think some features are unavailable, that is, unless I disallow ARC within the class I am currently wanting to set the precision of the NSNumber.
I understand NSNumberFormatter is the basic setPrecision class function I must use but I am having issues....
Everytime i use NSNumberFormatter, it spits back NULL!!!
nowValue = [now objectForKey:@"value"]; // This is what IM ACTUALLY DOING!!!
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[formatter setFormatWidth:2];
NSString *string = [formatter stringFromNumber:num];
I feel like this is SOOO trivial! Can anyone give an amateur programmer some advise ? :)
EDIT:
This is what I'm doing now, using an NSDecimal rather than NSNumber. "Now" is a large dictionary object and nowValue is a copy of num.
nowValue = (__bridge NSDecimal *)([now objectForKey:@"value"]);
Upvotes: 1
Views: 1980
Reputation: 1632
Problem Solved!..... Thanks for everyones input but yea.... it was a string. Martin R, thanks for you debugging code. And understanding _NSCFString is really just a wrapper for an NSString but they were mainly the same.
Here was my solution...
nowValue outputs : 24.14295959472
nowValue = [now objectForKey:@"value"];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * nowNum = [f numberFromString:nowValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
nowValueFormatted = [formatter stringFromNumber:nowNum];
nowNum outputs : 24.14
Upvotes: 1