Re: Repetitive Appending of Strings
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