Skip navigation.
 
mlRe: Repetitive Appending of Strings
FROM : Adam P Jenkins
DATE : Tue Feb 12 20:33:50 2008

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: @"%%%df", 
> numberOfDigits], n]
>
> which will make the format string dynamically.  So if numberOfDigits 
> were 1000, it would be equivalent to [NSString stringWithFormat: 
> @"%1000f", 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_removed>
> <http://www.gandreas.com/> wicked fun!
> quadrium | prime : build, mutate, evolve, animate : the next 
> generation of fractal art
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlRepetitive Appending of Strings Andrew Merenbach Feb 12, 17:47
mlRe: Repetitive Appending of Strings glenn andreas Feb 12, 19:06
mlRe: Repetitive Appending of Strings John Stiles Feb 12, 19:10
mlRe: Repetitive Appending of Strings Andrew Merenbach Feb 12, 19:21
mlRe: Repetitive Appending of Strings Michael Ash Feb 12, 19:22
mlRe: Repetitive Appending of Strings John Stiles Feb 12, 19:44
mlRe: Repetitive Appending of Strings Adam P Jenkins Feb 12, 19:53
mlRe: Repetitive Appending of Strings glenn andreas Feb 12, 20:05
mlRe: Repetitive Appending of Strings Jens Alfke Feb 12, 20:21
mlRe: Repetitive Appending of Strings Adam P Jenkins Feb 12, 20:33
mlRe: Repetitive Appending of Strings Andrew Merenbach Feb 12, 20:48
mlRe: Repetitive Appending of Strings Adam P Jenkins Feb 12, 22:07
mlRe: Repetitive Appending of Strings John Stiles Feb 12, 23:01
mlRe: Repetitive Appending of Strings Adam P Jenkins Feb 12, 23:51
mlRe: Repetitive Appending of Strings Adam P Jenkins Feb 13, 00:25
mlRe: Repetitive Appending of Strings Chris Suter Feb 13, 00:49
mlRe: Repetitive Appending of Strings Jean-Daniel Dupas Feb 13, 00:54
mlRe: Repetitive Appending of Strings Adam P Jenkins Feb 13, 01:21