How to pretty print floating numbers (without loosing precision)?
How to pretty print floating numbers (without loosing precision)?
- Subject: How to pretty print floating numbers (without loosing precision)?
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 11 Oct 2014 15:04:11 +0700
UInt64 sum = 16494631536958187520UL;
double doubleSum = sum;
NSString *strSum = [ NSNumberFormatter localizedStringFromNumber: @(doubleSum)
numberStyle: NSNumberFormatterDecimalStyle
];
NSLog(@"%s DecimalStyle (of double): %@; UInt64: %llu, double: %.22g",__FUNCTION__, strSum, sum, doubleSum );
This prints:
DecimalStyle (of double): 16,494,631,536,958,200,000; UInt64: 16494631536958187520, double: 16494631536958187520
Note: converting to double does NOT loose any digits. But NSNumberFormatter does. Why?
I also tried:
NSNumberFormatter *nf = [ [ NSNumberFormatter alloc ] init ];
[ nf setNumberStyle: NSNumberFormatterDecimalStyle ];
[ nf setUsesSignificantDigits: YES ];
[ nf setMaximumSignificantDigits: 22 ];
NSString *str2Sum = [ nf stringFromNumber: @(doubleSum) ];
But the result is the same.
Gerriet.
_______________________________________________
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