• 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
Re: Repetitive Appending of Strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Repetitive Appending of Strings


  • Subject: Re: Repetitive Appending of Strings
  • From: Adam P Jenkins <email@hidden>
  • Date: Tue, 12 Feb 2008 14:33:50 -0500

Ah, you can do that with only a single stringWithFormat: call, since the precision can be passed as an argument to format as well by using * as the precision in the format string. For example

int scale = 4;
float f = 42.4242;
NSString *string = [NSString stringWithFormat:@"%.*f", scale, f];

is the same as

NSString *string = [NSString stringWithFormat:@"%.4f", f];

except the scale isn't hardcoded.


On Feb 12, 2008, at 2:05 PM, glenn andreas wrote:
If what you're trying to do is to basically allow the user to specify (at run time) the precision of the number, you could just do:

[NSString stringWithFormat: [NSString stringWithFormat: @"%%ß", numberOfDigits], n]

which will make the format string dynamically. So if numberOfDigits were 1000, it would be equivalent to [NSString stringWithFormat: @"00f", n] (with the exact format string with regards to decimal places and the like left as an exercise for the reader and time spent with the numeric formating page). And of course, asking for even 100 decimal places is pointless, since a double has approximately 16 decimal digits of precision, with long double giving around 34...

Alternately look into NSNumberFormatter for more formatting options than you can shake a significant digit at.


Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next generation of fractal art




_______________________________________________

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

_______________________________________________

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


References: 
 >Repetitive Appending of Strings (From: Andrew Merenbach <email@hidden>)
 >Re: Repetitive Appending of Strings (From: glenn andreas <email@hidden>)
 >Re: Repetitive Appending of Strings (From: John Stiles <email@hidden>)
 >Re: Repetitive Appending of Strings (From: Andrew Merenbach <email@hidden>)
 >Re: Repetitive Appending of Strings (From: glenn andreas <email@hidden>)

  • Prev by Date: Crash at THFSPlusStore::LessThan(HFSUniStr255 const&)
  • Next by Date: Re: @property problem
  • Previous by thread: Re: Repetitive Appending of Strings
  • Next by thread: Re: Repetitive Appending of Strings
  • Index(es):
    • Date
    • Thread