• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to pretty print floating numbers (without loosing precision)?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: How to pretty print floating numbers (without loosing precision)?
      • From: Richard Charles <email@hidden>
  • Prev by Date: Re: How to pretty print (big)numbers?
  • Next by Date: Re: Questions on using a NSTextView as a source viewer.
  • Previous by thread: Re: How to pretty print (big)numbers?
  • Next by thread: Re: How to pretty print floating numbers (without loosing precision)?
  • Index(es):
    • Date
    • Thread