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>
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>






Cocoa mail archive

