NSNumberFormatter Strangeness
NSNumberFormatter Strangeness
- Subject: NSNumberFormatter Strangeness
- From: Conrad Shultz <email@hidden>
- Date: Thu, 24 Nov 2011 13:42:49 -0800
Greetings,
(Happy Thanksgiving, to those in countries which celebrate it.)
I am experiencing a strange behavior with NSNumberFormatter. For
reasons that aren't really relevant to the matter at hand, I wanted to
create a formatter that could support as precise a fraction as possible.
-setMaximumFractionDigits: takes an NSUInteger, so I tried using
NSUIntegerMax as the argument. But I got *no* fraction digits. After
some experimentation and a programmatic search, I determined that
100001871036415 is the largest value that gives reasonable results in
the various -setMaximum...: methods. Consider the following, with
output interleaved as comments beneath respective NSLog() calls:
NSNumber *aNumber = [NSNumber numberWithDouble:123.456];
NSNumberFormatter *testFormatter = [[NSNumberFormatter alloc] init];
[testFormatter setMaximumFractionDigits:3];
NSLog(@"max frac digits now %lu", [testFormatter maximumFractionDigits]);
NSLog(@"3 frac digits: %@", [testFormatter stringFromNumber:aNumber]);
//2011-11-24 13:17:16.651 FormatterTest[24113:707] max frac digits now 3
//2011-11-24 13:17:16.653 FormatterTest[24113:707] 3 frac digits: 123.456
[testFormatter setMaximumFractionDigits:100001871036415];
NSLog(@"max frac digits now %lu", [testFormatter maximumFractionDigits]);
NSLog(@"100001871036415 frac digits: %@", [testFormatter
stringFromNumber:aNumber]);
//2011-11-24 13:17:16.653 FormatterTest[24113:707] max frac digits now
100001871036415
//2011-11-24 13:17:16.654 FormatterTest[24113:707] 100001871036415 frac
digits: 123.456
[testFormatter setMaximumFractionDigits:100001871036416];
NSLog(@"max frac digits now %lu", [testFormatter maximumFractionDigits]);
NSLog(@"100001871036416 frac digits: %@", [testFormatter
stringFromNumber:aNumber]);
//2011-11-24 13:17:16.655 FormatterTest[24113:707] max frac digits now
100001871036416
//2011-11-24 13:17:16.655 FormatterTest[24113:707] 100001871036416 frac
digits: 123
[testFormatter setMaximumFractionDigits:100];
NSLog(@"max frac digits now %lu", [testFormatter maximumFractionDigits]);
NSLog(@"100 frac digits: %@", [testFormatter stringFromNumber:aNumber]);
//2011-11-24 13:17:16.656 FormatterTest[24113:707] max frac digits now 100
//2011-11-24 13:17:16.657 FormatterTest[24113:707] 100 frac digits: 123.456
[testFormatter setMaximumFractionDigits:NSUIntegerMax];
NSLog(@"max frac digits now %lu", [testFormatter maximumFractionDigits]);
NSLog(@"NSUIntegerMax frac digits: %@", [testFormatter
stringFromNumber:aNumber]);
//2011-11-24 13:17:16.657 FormatterTest[24113:707] max frac digits now
18446744073709551615
//2011-11-24 13:17:16.658 FormatterTest[24113:707] NSUIntegerMax frac
digits: 123
As you can see, the formatter is internally storing the correct value
for maximumFractionDigits.
So, my questions are:
1) Is this a bug or is it expected behavior that I should have known?
2) What is the significance of 100001871036415? It's not a max for any
common data type as far I can recall, it's not close to a power of 2 (if
I did my math correctly, it's approximately 2 ^ 46.51), and Googling the
number itself turns up nothing.
(This is using the OS X SDK, on 10.7.2/Xcode 4.2.1, targeting Lion,
Intel Core i5 processor. Tested under both ARC and MRC.)
Thanks.
--
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden